0

I am a python newbie and have written a short program. The first part works but the if statement part has a traceback/syntax? problem. Advice?

hours = input("How many hours did you work this week?")
wage = input("How much do you make each hour?")
weeklySalary = hours * wage
print "You made", weeklySalary, "dollars this week."
daily = str(input("Would you like to know your daily average this week?"))
if daily in ['Yes' , 'yes' , 'Y' , 'y']:
    print "You averaged", (weeklySalary / 7), "dollars per day."
else:
    print "Maybe next week..."

Here is the error:

How many hours did you work this week?10
How much do you make each hour?10
You made 100 dollars this week.
Would you like to know your daily average this week?yes
Traceback (most recent call last):
  File "/Users/jake/Desktop/Python_U_M/weekly_salary.py", line 5, in <module>
    daily = str(input("Would you like to know your daily average this week?"))
  File "<string>", line 1, in <module>
NameError: name 'yes' is not defined
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
JakeG
  • 13
  • 3
  • 4
    Presumably this is Python 2.x and you should therefore be using `raw_input`. However, it is generally helpful to provide the full error traceback rather than a vague problem description. – jonrsharpe Jun 13 '14 at 21:53
  • 1
    there is no syntax error there ... – Joran Beasley Jun 13 '14 at 21:54
  • 1
    It'd be useful to show the syntax error you're getting. – babbageclunk Jun 13 '14 at 21:54
  • @JoranBeasley: but if you entered 'Y' a `NameError` will be raised.. – Martijn Pieters Jun 13 '14 at 21:55
  • I'm using Python 3 and there's no problem. What sort of syntax error does it say is happening? – Epiglottal Axolotl Jun 13 '14 at 21:55
  • @MartijnPieters Of coarse ... but that is certainly not a syntax error – Joran Beasley Jun 13 '14 at 21:55
  • 1
    Plenty of dupes: ["NameError: name 'n' is not defined" even though it's an input - Python](http://stackoverflow.com/q/17413502), [Python input() error - NameError: name '...' is not defined](http://stackoverflow.com/q/21122540), [NameError: name 'book' is not defined](http://stackoverflow.com/q/11280194), [Python, name not defined](http://stackoverflow.com/q/6163596), [NameError from Python input() function](http://stackoverflow.com/q/16457441), [NameError: name 'now' is not defined](http://stackoverflow.com/q/15190632) – Martijn Pieters Jun 13 '14 at 22:05
  • Some more: [Problem running a Python program, error: Name 's' is not defined](http://stackoverflow.com/q/2790565), [New to Python: getting NameError in variable](http://stackoverflow.com/q/6796513), [Input data not defined](http://stackoverflow.com/q/11412409), [Name is not defined error](http://stackoverflow.com/q/9315542), [Getting a name error when trying to input a string](http://stackoverflow.com/q/19887358), [error in python d not defined](http://stackoverflow.com/q/2612948) – Martijn Pieters Jun 13 '14 at 22:07

1 Answers1

2

The problem is that input is evaluating your input, so eval(y) is raising the error:

How many hours did you work this week?10
How much do you make each hour?7
You made 70 dollars this week.
Would you like to know your daily average this week?y
Traceback (most recent call last):
  File "hmm", line 5, in <module>
    daily = str(input("Would you like to know your daily average this week?"))
  File "<string>", line 1, in <module>
NameError: name 'y' is not defined

Compare to:

How many hours did you work this week?10
How much do you make each hour?7
You made 70 dollars this week.
Would you like to know your daily average this week?"y"
You averaged 10 dollars per day.

Docs: https://docs.python.org/2/library/functions.html#input

As noted in the docs, "Consider using the raw_input() function for general input from users." Making this change prevents 'y' from being evaluated, thus treating is as a string, like you're expecting.

The problem doesn't manifest on the integers because eval(10) is still 10.

Confirmed via Python 2.6.5. Your code would likely work as-is in Python 3 -- docs for input in Python 3 do not include the implicit eval: https://docs.python.org/3/library/functions.html#input

DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71
  • How about `eval(010)` being treated as octal and result will be `8`? – Aamir Rind Jun 13 '14 at 22:01
  • Thanks. I changed input to raw_input and it worked. Should I be using raw_input everywhere - even when expecting in integer? – JakeG Jun 13 '14 at 22:02
  • It has to be quoted -- `eval('010')` evaluates to 8, whereas `eval(010)` raises a TypeError. But sure. :-) – DreadPirateShawn Jun 13 '14 at 22:02
  • @DreadPirateShawn but I was thinking in terms of `raw_input` or `input` but you are right. – Aamir Rind Jun 13 '14 at 22:05
  • Jake -- the docs are your guide. The "consider using raw input" advice is already copied in my answer... then, given that `input(prompt)` equates to `eval(raw_input(prompt))`, the question for you is, "Do you want the user's input to be evaluated?" https://docs.python.org/2/library/functions.html#eval That is -- do you want a user to be able to input `10 + 4` to convey that they worked 14 hours? Don't `eval` things unless you want them to be `eval`ed. – DreadPirateShawn Jun 13 '14 at 22:06