+ 4
can anyone please tell me what is the mistake?
6 Answers
+ 3
The code works when line 15 and 16 is indented.
You can also inline a break statement to stop the program from looping.
while True:
print("Options:")
print("Enter 'add' to add two numbers")
print("Enter 'subtract' to subtract two numbers")
print("Enter 'multiply' to multiply two numbers")
print("Enter 'divide' to divide two numbers")
print("Enter 'quit' to end the program")
user_input = input(": ")
if user_input == "quit":
break
elif user_input == "add":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
result = str(num1 + num2)
print("The answer is " + result)
break
+ 3
The while loop is used to do more than one calculation. so you can say "add" and then 5 and the other value 7, so result will show 12. And it will immediately be ready for a new calculation. If input is "quit" the program will be terminated.
+ 2
VCX king! it worked very well!
+ 1
i am not entirely sure why, but your problem seems to be in the break
or perhaps the while
why are you using a while true?
+ 1
Elie Aro đ Thanks bud đ
0
Cbrâ[ Most active ] thanks! I noticed that, but donât know why. can you maybe explain why one would use a while statement here? also do you know why it doesnât work? just for my own edification :)