+ 2

Simple Calculation Program

I have got 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: ")) result = str(num1 + num2) print("The answer is " + result) else: print("Unknown input") When I run the following Programm in the Terminal, I get it to run only if the user enters the word add between '' as this: 'add' is there anway to change the program, so that the user get to input only the word as it is without "" or '' and still get it to run without erors? Here is the eror message that I get each time I enter the word add without ' '. Traceback (most recent call last): File "ifelse.py", line 21, in <module> user_input = input(":") File "<string>", line 1, in <module> NameError: name 'add' is not defined

5th Nov 2019, 11:05 AM
Layla Di
Layla Di - avatar
8 Antworten
+ 6
hi, i did some trials with your code and i can say that it runs as *expected* in a regular IDE. I used add without quotes. If i use add with quotes, it does no calculation and returns *Unknown input* which comes from the else clause in your code. What is not work ing are the operations: subtract, multiply, divide, because it is not implemented to check for this input, and also the calculation for this operations are not implemented . To get it run in Playground, it needs a different input procedure, but i think this is not your problem. So it's not clear what causes the problem you mentioned.
5th Nov 2019, 12:14 PM
Lothar
Lothar - avatar
+ 3
thanks. clearly after reading your reply the problem could be using the mac terminal ...which idle do u use?
5th Nov 2019, 12:17 PM
Layla Di
Layla Di - avatar
+ 3
I have used pythonista on iOS and spyder on windows.
5th Nov 2019, 2:53 PM
Lothar
Lothar - avatar
+ 2
Layla Di I mostly use sublime text 3 for long codes, but for simple scripts I use nvim in terminal because it's faster.
9th Nov 2019, 4:19 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Layla Di Atom is good, I used to use it a lot, but sublime text has way more packages and is more customizable
15th Nov 2019, 1:19 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
There shouldn't be any difference whether you're working with IDLE or just a normal terminal, the code you provided works fine on my terminal. Make sure this is the exact same code you wrote.
9th Nov 2019, 4:13 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
as soon as copy past works...but i will check again. Thanks. For the sake of mother curiosity: what text editor do u use for python Aymane ?
9th Nov 2019, 4:18 PM
Layla Di
Layla Di - avatar
0
#Aymane Boukrouh what do u think about ATOM?
15th Nov 2019, 10:55 AM
Layla Di
Layla Di - avatar