NameError:未定义名称"April"

0 python

我知道这是愚蠢的,但我无法弄清楚为什么它不起作用.请帮忙.

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()
Run Code Online (Sandbox Code Playgroud)

追溯:

 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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

使用raw_input(),而不是input(); 后者试图将文本输入解释为Python代码.

April在这种情况下,键入被解释为变量名称.你可以输入,"April"但最好不要input()在这里使用.