0
why am i get error in this program?
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: ")) print(float(num1+num2)) break elif user_input == "subtract": ... elif user_input == "multiply": ... elif user_input == "divide": ... else: print("Unknown input")
6 ответов
+ 1
follow that also for divide and all conditions should work perfectly i hope this helps you
+ 2
The break inside the 'add' elif is not properly indented... Furthermore, shouldn't it be removed, in order to continue into the while(True) loop?
+ 1
I copied and pasted this into writer to make a txt file on my phone... then bluetoothed that to my PC and then copied and pasted it into atom.io... if the indentation didnt get messed up somewhere in transferring the file around.. you had multiple indentation issues. I went through and fixxed each line.. the program that you have written so far runs just fine.
+ 1
i also have ran your code in my pycharm ide and the truth is you have indented it wrongly two break statement are wrongly indented,
elif user_input == "subtract":
...
elif user_input == "multiply":
...
elif user_input == "divide":
...
this would definitely throw error cause there should be a task(statement) after the conditional statement if, you could just pass it if you don't want to put anything there for now so it would look like this :
elif user_input == "subtract":
pass
elif user_input == "multiply":
pass
elif user_input == "divide":
pass
else:
print("Unknown input")
this should fix the error but note that if the user should input
"subtract",
"multiply",
"divide",
then nothing happens and the while loop doesn't break i.e the loop is restarted from the first line
Options:
Enter 'add' to add two numbers......
cause you just passed it, you need to put something there so it looks just like the "add" condition only the operator is different, something like this :
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: "))
print(float(num1+num2))
break
elif user_input == "subtract":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
print(float(num1 - num2))
break
elif user_input == "multiply":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
print(float(num1 * num2))
break
elif user_input == "divide":
0
what error exactly are you getting and what IDE are you using to run your code if you could please state this then i can help you , and looking at your code the other conditional statement has no task to carry out if a user were to input subtract then what happens
0
thanks to all