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
8

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

How about eval(010) being treated as octal and result will be 8?
Thanks. I changed input to raw_input and it worked. Should I be using raw_input everywhere - even when expecting in integer?
It has to be quoted -- eval('010') evaluates to 8, whereas eval(010) raises a TypeError. But sure. :-)
@DreadPirateShawn but I was thinking in terms of raw_input or input but you are right.
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?" 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 evaled.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.