Hey i need help with this python code. Im trying to make a simple caluculater but python keeps giving me sintax errors. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
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