0

Syntax error in basic calculator need help

Hello guys I'am having a syntax error on my calculator on elif line can you tell me where i am wrong. while True: print("Options") print("Enter'add' to add two numbers") print("Enter'subtract' to subtract two number") print("Enter 'multiply' to multiplty to multiplty 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) elif user_input =='subtract': num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1-num2) print("The answer is + result") elif user_input =='multiply': num1 =float(input("Enter a number.")) num2 =float(input("Enter another number.")) result = str(num1 * num2) print("The answer is + result") elif user_input =='divide': num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1 / num2) print("The answer is + result") else : print("Unknown input")

16th Jul 2017, 2:54 AM
ERGELEN Ugur
ERGELEN Ugur - avatar
14 Antworten
+ 7
P.S. Here is a calculator in one line :) print(eval(input("Enter math expression to evaluate.\n"))) https://code.sololearn.com/cx1TgWN410RL
16th Jul 2017, 5:22 AM
David Ashton
David Ashton - avatar
+ 5
I pasted your code into Pycharm and found out some of your indents seem to be tabs and others use spaces. Here it is with the tabs changed to spaces and the minor print problem fixed (see notes at the end) while True: print("Options") print("Enter'add' to add two numbers") print("Enter'subtract' to subtract two number") print("Enter 'multiply' to multiplty to multiplty 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) elif user_input =='subtract': num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1-num2) print("The answer is "+ result) elif user_input =='multiply': num1 =float(input("Enter a number.")) num2 =float(input("Enter another number.")) result = str(num1 * num2) print("The answer is "+ result) elif user_input =='divide': num1 = float(input("Enter a number.")) num2 = float(input("Enter another number.")) result = str(num1 / num2) print("The answer is "+ result) else : print("Unknown input") ''' 1. Convert all tabs to spaces, 4 spaces for each indent level. 2. print("The answer is + result") should be print("The answer is " + result) 3. correct typos (multiplty to multiply) 4. suggestion: since it is a hassle to type "multiply", you could just ask the user to enter 'a', 's', 'm', 'd' instead of the full words. '''
16th Jul 2017, 4:53 AM
David Ashton
David Ashton - avatar
+ 4
First and formost, Python uses "elif" instead of "else if". Other than that, it might be something more hidden.
16th Jul 2017, 2:33 AM
Keto Z
Keto Z - avatar
+ 3
Which line is the error on?
16th Jul 2017, 2:51 AM
Keto Z
Keto Z - avatar
0
I tried it with first elif than tried else if but as if I recall right else if and elif basicly does the same thing.
16th Jul 2017, 2:37 AM
ERGELEN Ugur
ERGELEN Ugur - avatar
0
It was 12 when I open the thread but now it's the 23 after a few tweaking but still I can't see what did I wrong :S
16th Jul 2017, 2:54 AM
ERGELEN Ugur
ERGELEN Ugur - avatar
0
I actually did that found the missing Sob but still
16th Jul 2017, 3:03 AM
ERGELEN Ugur
ERGELEN Ugur - avatar
0
you don't close the quotes printing variables you should do this print("the answer is "+result)
16th Jul 2017, 3:07 AM
Andrés04_ve
Andrés04_ve - avatar
0
I fixed it too but soloLearn still show to you guys my first code not the editing version
16th Jul 2017, 3:07 AM
ERGELEN Ugur
ERGELEN Ugur - avatar
0
Yes exactly
16th Jul 2017, 3:19 AM
ERGELEN Ugur
ERGELEN Ugur - avatar
0
maybe the erro would be caused by the indented row of return, sometime its caused if is used many spaces instead the tab key, look for other rows too, not only return row. i wish you good coding.
16th Jul 2017, 4:05 AM
Antônio Alexandre Neto
Antônio Alexandre Neto - avatar