0
Issues with Python code
I've just finished writing up the given code for a basic calculator, I've had to move stuff around etc, but I just cannot figure out what is wrong. I've uploaded it into my public codes so hopefully someone here may be able to help me. Thank you in advance dudes and dudettes.
3 odpowiedzi
+ 4
while True:
# all in the while loop
# start by print instruction
print("Use 'Add' to add two numbers together")
print("Use 'Minus' to take the second number away from the first")
print("Use 'Multiply' to multiply two numbers together")
print("Use 'Divide' to divide the first number by the second")
print("Use 'Quit' to stop the calculator")
# ask for operation and test if valid one
user_input=input(":")
user_input=user_input.lower() # allows ignore case for keywords
if user_input=='quit':
break
# exit the while loop
elif user_input not in ['add','minus','multiply','divide']:
print("Unknown Input")
continue # jump to the while loop start
# ask for numbers and do operation only if valid operation
num1=float(input("Enter a number:"))
num2=float(input("Enter another number:"))
if user_input=='add':
result=str(num1+num2)
elif user_input=='minus':
result=str(num1-num2)
elif user_input=='multiply':
result=str(num1*num2)
elif user_input=='divide':
result=str(num1/num2)
# and finally output the result
print("The answer is:" + result)
+ 5
The only code inside the while loop is the print statement. You have to indent the if-elif-else statement if you want it to be inside the loop.
You are starting the if statement with an elif statement. That will not work too.
Start by correcting those.
0
thank you!