Если оператор не выходит из цикла, как я ожидалPython

Программы на Python
Anonymous
Если оператор не выходит из цикла, как я ожидал

Сообщение Anonymous »


Это мой код функции (где, вероятно, происходит ошибка):

Код: Выделить всё

def showAddition():
print("You have selected: \"Addition\"")

while True:
last_answer = None
if last_answer:
next_num = int(input("Please input another number: "))
answer = calc.addNums(last_answer, next_num)
else:
num1 = int(input("Please enter your first number: "))
num2 = int(input("Please enter your second number: "))
answer = calc.addNums(num1, num2)

print(f'Your answer is {answer}')
continue_prompt = input("Would you like to continue? Y/N\n")

if continue_prompt.upper() == "Y" or continue_prompt.upper() == "YES":
last_answer = answer
else:
break
I was trying to make it so that I could enter another number immediately and add number after number without fail. However, for some reason, the loop simply repeats. It doesn´t even break, even if I input "no" into the continue prompt. Both "yes" and "no" result in the exact same result, and i don't get it.


Источник: https://stackoverflow.com/questions/781 ... uld-expect

Вернуться в «Python»