+ 1

Hey i need help with this python code. Im trying to make a simple caluculater but python keeps giving me sintax errors.

the error is file "<pyshell#0>", line 2 while true: ^ my code is : while true: print(“Options:”) print(“Enter ‘add’ to add two numbers together”) print(“Enter ‘substract’ to substract from a number”) print(“Enter ‘multiply’ to multiply a number”) print(“Enter ‘divide’ to divide a number”) print(“Enter ‘quit’ to quit the program”) 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 == “substract”: 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 numb

19th Aug 2018, 3:31 AM
Matthew Molski
Matthew Molski - avatar
6 odpowiedzi
0
I assume you’ve forgotten to tab after your ‘while’, ‘if’ and ‘for’ statements. Python is very strict on tabbing, so make sure after any of those, the corresponsing block is tabbed. I also suggest that when you next ask a question - include the actual error thrown. I assume in this case it said “Expected indented block after...” Another tip from looking at this, you could take your input reading out of your ‘if’ statements. So read in the 2 numbers, then do the work, rather than repeating the same code 4 times. Anytime you are writing the exact same thing multiple times - question if you could reduce it, makes code much easier to add to or maintain that way. Your outputting of the result could also be after all the ‘if’s.
19th Aug 2018, 8:39 AM
Charlie
+ 1
thx a lot I will try again
19th Aug 2018, 3:40 PM
Matthew Molski
Matthew Molski - avatar
+ 1
although this opens another question for me. I've searched up what tabs are and cant get anything about them.
19th Aug 2018, 4:16 PM
Matthew Molski
Matthew Molski - avatar
0
matt I mean literally hit tab or space 4 times so the code is indented over.
19th Aug 2018, 6:32 PM
Charlie
0
thx a lot I appreciate it a lot
19th Aug 2018, 7:13 PM
Matthew Molski
Matthew Molski - avatar
0
Fill in the blanks to make the calculator work for multiplication. elif user_input == "multiply": num1 = float(input("Enter a number: ")) num2 = (input("Enter another number: ")) result = str(num1 num2) print("The answer is " + )
6th Mar 2020, 2:15 AM
Ahmed Ansari
Ahmed Ansari - avatar