0

I have been using python on eclipse and I have been having trouble when I ask a question and the response includes letters(number answers work). for example if I ask

x=input("what is your name")
print(x)

this will give me an error message similar to the one on the bottom. below is something I was trying out with the same error as the above code.

x=input("enter a number")
y=input("enter a second number")
z=input("add, divide, or multiply?")

if(z == "add"):
    print(x+y)

if(z == "divide"):
    print(x/y)

if(z == "multiply"):
    print(x*y)

else:
    print("error")

below is what i get once i get to the part where i have to choose "add, divide, or multiply"

Traceback (most recent call last):
  File "/Users/jacobmaldonado/Desktop/untitled folder/abc/src/def.py", line 10, in <module>
    z=input("add, divide, or multiply?")
  File "/Users/jacobmaldonado/Downloads/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev_4.2.0.201507041133/pysrc/pydev_sitecustomize/sitecustomize.py", line 141, in input
    return eval(raw_input(prompt))
  File "<string>", line 1, in <module>
NameError: name 'add' is not defined
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Morgan Thrapp Jul 20 '15 at 15:15
  • 3
    https://en.wikibooks.org/wiki/Python_Programming/Input_and_Output#input.28.29 : `input() uses raw_input to read a string of data, and then attempts to evaluate it as if it were a Python program` – Marc B Jul 20 '15 at 15:17
  • 1
    use `raw_input` instead of `input` – NightShadeQueen Jul 20 '15 at 15:18
  • Make sure you are using else ifs correctly, the way you have it will always output "error" when not multiplying. you want to use `elif` statements: http://www.tutorialspoint.com/python/python_if_else.htm – Snappawapa Jul 20 '15 at 15:22
  • 1
    Someone gave a down vote to this person who is new to programming. This is not an obvious question for someone new. Could the answer have been found elsewhere? Sure. But then, by extending that logic, any question on stackoverflow should be downvoted, because in principle you can always find the answer somewhere. The threshold for when a question is obvious is a subjective threshold. The downvoter him/herself never asked a question like this? Sure they did at some point at least seen from the view of, say, a hacker. I merely suggest trying to be more open-minded to create a stronger community. – Jesper - jtk.eth Jul 20 '15 at 15:24

1 Answers1

1

In Python 2.x, input tries to evaluate the string that is typed as a Python expression, making it equivalent to

z = eval(raw_input("add, divide, or multiply?"))

You want to use raw_input instead, which simply returns the string that was typed.

In Python 3.x, there is only one function, input, which is really raw_input renamed.

chepner
  • 497,756
  • 71
  • 530
  • 681