Simple Calculator exercise | Sololearn: Learn to code for FREE!

0

Simple Calculator exercise

Hey all i keep getting this error whenever i try to run the code even tho at some point i just copied and pasted the code into my code editor. > while True: print("Options") print("Enter 'add' to add two numbers") print("Enter 'substract' to substract 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("Enter your operation:") 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("Answer -> " + result) elif user_input== "substract": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 - num2) print("Answer -> " + result) elif user_input== "multiply": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 * num2) print("Answer -> " + result) elif user_input== "divide": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 / num2) print("Answer -> " + result) else: print("Unknown input, sorry try again") This is the error im getting: >>> python 1.py Options Enter 'add' to add two numbers Enter 'substract' to substract two numbers Enter 'multiply' to multiply two numbers Enter 'divide' to divide two numbers Enter 'quit' to end the program Enter your operation:add Traceback (most recent call last): File "1.py", line 9, in <module> user_input = input("Enter your operation:") File "<string>", line 1, in <module> NameError: name 'add' is not defined

7/7/2019 6:48:51 PM

Enne

10 Answers

New Answer

+1

Enne Sounds like a problem with your IDE (VStudio) then. As long as you used the exact same code as you pasted in the Sololearn program, it should work. Are you using Python3? Maybe you have to use raw_input instead of input. If you still have problems, just use the built-in Python editor when you install it to your computer.

+1

https://code.sololearn.com/cJ8svIMkUUMe/?ref=app https://code.sololearn.com/c8Ft7cFhmB65/?ref=app get help from these two codes and I will see what error is going on

+1

I have found the error 1.in first line > is not to be used 2.your code is too long 3.it is messy 4.every time I read it a new problem occurs like object being not defined properly 5.haven't make use of loop well 6.indentation  errors All this is failure to your code

+1

@Frenchtoast Hey mate thanks a lot... Using <raw_input> when defining the variable user_input it worked like a charm... Thanks a lot again, now im gonna search where the main difference of those radicate.

0

Could you put this program into a code file on your profile? I can't see the error right now, but if I can run it, I might be able to spot it.

0

Done, thanks for the reply... When i run it on the sololearn code editor it tells me that i dont have input... but dont understand why? :/ Anyways if you can give me some input about it that would be much appreciate it. https://code.sololearn.com/cUV67sBv6OSz

0

I'm a n00b and what i did was simply copied the code provided by the 1st Lesson's Final Exercise and twerking it just a little bit... Kindda sucks that all the solutions and exercises posted to new people look that bad to you. I will look into your code, thanks a lot for the heads up.

0

And next time whenever you (Enne ) want to copy the code , please read it carefully because some code also has virus detached in them that is hard to find by a pupil like you. Don't mean I am retarding your success.But if you won't realise it will be harder for you. Basic is very important for these codes :)

0

The program you made on Sololearn works perfectly. It just asks for the input ahead of time. For a working demonstration, when it asks for input: type add, then type 1 on the second line and 2 on the third line. It will only error at the end because this is running infinitely and you aren't giving it infinite input.

0

Hey @Frenchtoast indeed whenever i run the code on the sololearn code editor it indeed let me do it smoothly, it simply asks me to inster the operation and then the 2 inputs(str). However I'm trying to code using VStudio and when i add the operation "add" for example this is what it prints: >>> Enter your operation:add Traceback (most recent call last): File "1.py", line 9, in <module> user_input = input("Enter your operation:") File "<string>", line 1, in <module> NameError: name 'add' is not defined