Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:NameError:未定义名称“female”_Python_Python 3.x - Fatal编程技术网

Python:NameError:未定义名称“female”

Python:NameError:未定义名称“female”,python,python-3.x,Python,Python 3.x,我正在学习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=="fem

我正在学习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)
错误是:

文件n13.py中的第3行 sex=inpunter人员的性别:文件,第1行,在NameError中:未定义名称“女性”

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


提前谢谢

您正在Python2而不是Python3上运行代码。在Python2中,对读取为Python代码的字符串求值,请参见

如果要使用Python 2,请将输入替换为

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

#!/usr/bin/python3

您没有使用Python3,因此在输入的瞬间必须使用原始输入,因此这非常有效:

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)

如果您的机器上有python 3,我建议您使用:/usr/bin/python3,然后代码与您的相同

我在python3.x上没有收到相同的错误,也许您正在Python2.x上运行它?这个错误可能发生在Python2.x上,因为两个版本的输入不同。如果您使用的是Python2.x,那么用raw_input替换输入就用Python2中的raw_input替换输入。另外,如果无效性别为true,则最后一个print语句中的pi是未定义的。如果复制并粘贴代码,则可能会重复,请查看!线它可能会说/usr/bin/python,但u被空格替换。