A problem with nested while loop and nested if/elif. English isn't my first language, sorry. I'll show the code after description so you could understand what I'm talking about.
I've started to study Python a few weeks ago. Today I've faced the issue with while loop and if/elif. I don't understand why it's not working the way I expected. I'm trying to make the program collect user's numbers input into 4 lists depending on the option they chose: sum, subtract, multiply and divide. I want the program to calculate choosen option with the list of numbers user did input. It seems to be working, but there is an unresolved question. I'm trying to make the program working in case of invalid input inside each nested while loops - no luck. The thing I call the invalid input is something other than a number or a keyword (sum, subtract, multiply, divide, quit.
import math
while True:
# Предложение выбрать нужную операцию.
option = input("\nВыберите программу: "
"\n1) Сложение "
"\n2) Вычитание "
"\n3) Умножение "
"\n4) Деление"
"\nВведите quit для выхода."
"\n> ")
option = option.lower()
# Переменные для вмещения вводимых чисел.
sum_list = [] # список для операции сложения
subtract_list = [] # список для операции вычитания
multiply_list = [] # список для операции умножения
divide_list = [] # список для операции деления
if option in ["1", "1)", "сложение"]:
print("Введите числа по одному, затем — «sum» для сложения.")
while True:
num = input("> ")
if num == "sum":
break
sum_list.append(float(num))
# Ввод операции сложения.
sum_result = sum(sum_list)
print(f"Результат операции сложения: {sum_result}")
elif option in ["2", "2)", "вычитание"]:
print("Введите числа по одному, затем — «subtract» для вычитания.")
while True:
num = input("> ")
if num == "subtract":
break
subtract_list.append(float(num))
# Ввод операции вычитания.
subtract_result = subtract_list[0] - sum(subtract_list[1:])
print(f"Результат операции вычитания: {subtract_result}")
elif option in ["3", "3)", "умножение"]:
print("Введите числа по одному, затем — «multiply» для умножения.")
while True:
num = input("> ")
if num == "multiply":
break
multiply_list.append(float(num))
# Ввод операции умножения.
multiply_result = math.prod(multiply_list)
print(f"Результат операции умножения: {multiply_result}")
elif option in ["4", "4)", "деление"]:
print("Введите числа по одному, затем — «divide» для деления.")
while True:
num = input("> ")
if num == "divide":
break
divide_list.append(float(num))
# Ввод операции деления.
divide_result = divide_list[0] / math.prod(divide_list[1:])
print(f"Результат операции деления: {divide_result}")
# Для выхода.
elif option == "quit":
print("Выход из программы...\n...завершён.")
exit()
# Неправильный ввод на этапе выбора программы.
else:
print("Вы не выбрали программу. Попробуйте заново.")
option = input("> ").lower()
continue
I've tried different things, I've spent the whole day - I just don't know how to make the program run further in case of invalid input inside each nested while loops. This is what I'm calling nested while loop:
if option in ["1", "1)", "сложение"]:
print("Введите числа по одному, затем — «sum» для сложения.")
while True:
num = input("> ")
if num == "sum":
break
sum_list.append(float(num))
# Ввод операции сложения.
sum_result = sum(sum_list)
print(f"Результат операции сложения: {sum_result}")
If I choose any option (sum, etc) and then enter some word instead of a number a keyword (sum, etc), I'll get this error:
Traceback (most recent call last):
File "myfile.py", line 27, in <module>
sum_list.append(float(num))
ValueError: could not convert string to float: 'this is the error if I enter something other than a number or a keyword (sum, subtract, multiply, divide, quit)'
try: sum_list.append(float(num)); except ValueError: print("Please provide a number")?continue(docs.python.org/3/tutorial/…). So if you find invalid input, maybe you want to print a message to the user, thencontinue.