0

I know it is something stupid but I cannot figure out why it does not work. Please help.

def main():
    yourAge=getAge() #get subject's age
    yourWeight=getWeight() #get subject's weight 
    yourBirthMonth=getMonth() #get subject's birth month
    correctAnswers(getAge, getWeight, getMonth)

def getAge():
    yourAge=input('Enter your age. ')
    return yourAge

def getWeight():
    yourWeight=input('Enter your weight.')
    return yourWeight

def getMonth():
    yourBirthMonth=input('Enter your birth month. ')
    return yourBirthMonth

def correctAnswers(getAge, getWeight, getMonth):
    if getAge <= 25:
        print'Congratulations, age is less than 25.'

    if getWeight >= 128:
        print'Congratulations, weight is more than 128.'

    if getMonth == 'April':
        print'Congratulations, month is April.'

main()

The traceback:

 Traceback (most recent call last):
  File "C:/Users/Beth/Documents/jeff/prog/lab 03/lab 3-5.py", line 35, in <module>
    main()
  File "C:/Users/Beth/Documents/jeff/prog/lab 03/lab 3-5.py", line 10, in main
    yourBirthMonth=getMonth()#get subject's birth month
  File "C:/Users/Beth/Documents/jeff/prog/lab 03/lab 3-5.py", line 22, in getMonth
    yourBirthMonth=input('Enter your birth month. ')
  File "<string>", line 1, in <module>
NameError: name 'April' is not defined
Falmarri
  • 47,727
  • 41
  • 151
  • 191

3 Answers3

5

Use raw_input(), not input(); the latter tries to interpret text input as Python code.

Typing April is interpreted as a variable name in that case. You could enter "April" instead but it's better to just not use input() here.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

In python2 input evaluates the input. Use raw_input to get unevaluated strings.

In your case input tries to lookup the variable april once you enter it, which for sure does not exist.

To visualize the effect, try entering 1/0 when promted for the age.


Basically your functions could look like this:

def getAge():
    return int(raw_input('Enter your age. '))

def getWeight():
    return int(raw_input('Enter your weight. '))

def getMonth():
    return raw_input('Enter your birth month. ')
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
0

Replace

yourBirthMonth=input('Enter your birth month. ')

To

yourBirthMonth=raw_input('Enter your birth month. ')

In Python 2, raw_input() returns a string, and input() tries to run the input as a Python expression

gprx100
  • 370
  • 1
  • 4
  • 13