Python:NameError:name female is not defined

2024-07-08 10:50:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在学习python,我还在做新手练习…所以有一个练习,我需要说的是人的身高的理想体重,考虑到它的类型。。。 所以我的代码是:

#!/ sr/bin/python
heigth = float(input("Enter the height of the person: "))
sex = input("Enter the person's gender:")

if(sex == "male"):
        pi = (72.7 * heigth) - 58
elif(sex=="female"):
        pi = (62.1 * heigth) - 44.7
else:
        print("Invalid gender")

print("The ideal weight for this person is:",pi)

错误是:

File "n13.py", line 3, in sex = input("Enter the person's gender:") File "", line 1, in NameError: name 'female' is not defined

我不明白为什么要定义比较中的字符串,如果它只是一个字符串???o、 o

提前谢谢!在


Tags: the字符串ininputislinepigender
2条回答

您在python2上运行代码,而不是python3。在python2中,^{}将它读取的字符串计算为Python代码(请参见this question)。在

如果您将使用python2,请将input替换为^{}。在

如果要使用Python 3,最好将shebang更改为:

#!/usr/bin/python3

您不是在使用python 3,因此即时输入必须使用原始输入,因此这非常有效:

heigth = float(input("Enter the height of the person: "))
sex = raw_input("Enter the person's gender:")

if(sex == "male"):
        pi = (72.7 * heigth) - 58
elif(sex=="female"):
        pi = (62.1 * heigth) - 44.7
else:
        print("Invalid gender")

print("The ideal weight for this person is:",pi)

如果您的机器上有python3,我建议您使用:#!/usr/bin/python3,然后代码和你的一样

相关问题 更多 >

    热门问题