gpt4 book ai didi

python - NameError:名称 'age' 未定义

转载 作者:太空宇宙 更新时间:2023-11-03 16:58:04 24 4
gpt4 key购买 nike

我已经研究了很长一段时间了,但我不知道哪里出了问题。我对 Python 很陌生,所以这可能是一个简单的修复。

def main():
age = 0;
weight = 0;
birthMonth = " ";
getAge();
getWeight();
getBirth();
correct();

def getAge():
age = input("Guess the age.\t")
return age;
def getWeight():
weight = input("Guess the weight.\t")
return weight;
def getBirth():
birthMonth = input("Guess the month.\t")
return birthMonth;
def correct():
if (age <= 25):
print ("Congratulations, the age is 25 or less")
else:
print ("You did not correctly guess the age");
if (weight <= 128):
print ("Congratulations, the weight is 128 or more")
else:
print ("You did not correctly guess the weight");
if (birthMonth == 25):
print ("Congratulations, the month is April")
else:
print ("You did not correctly guess the month");
main();

在空闲状态下运行它后出现的错误是,回答三个提示如下:

Traceback (most recent call last):
line 39, in <module>
main();
line 9, in main
correct();
line 24, in correct
if (age <= 25):
NameError: name 'age' is not defined

请帮忙,或者将我发送到有帮助的帖子。我花了大约一个小时试图找到这样的东西。

最佳答案

你尝试过这个吗?

def main():
age = getAge()
weight = getWeight()
birthMonth = getBirth()
correct(age,weight,birthMonth)

def getAge():
age = float(input("Guess the age.\t"))
return age
def getWeight():
weight = float(input("Guess the weight.\t"))
return weight
def getBirth():
birthMonth = input("Guess the month.\t")
return birthMonth
def correct(age,weight,birthMonth):
if age <= 25:
print ("Congratulations, the age is 25 or less")
else:
print ("You did not correctly guess the age")
if weight <= 128:
print ("Congratulations, the weight is 128 or more")
else:
print ("You did not correctly guess the weight")
if birthMonth == "April":
print ("Congratulations, the month is April")
else:
print ("You did not correctly guess the month")

main()

关于python - NameError:名称 'age' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com