Input 我无法将输入定义为Y(是),我收到回溯调用 这是一个作业 当涉及到怀孕是否怀孕的问题时=Y如果不是N 它把我扔回去了Y没有定义?因为它区分大小写。 --------------------------------------------------------------------------- >名称错误 >回溯(最近一次呼叫最后一次) >在 >16如果年龄>18岁,性别=“F”: >17怀孕=输入(“你怀孕了(Y)es还是(N):”)。上限() >--->18如果怀孕==Y.upper(): >19打印(“你需要一张医生的便条,你可以飞:”) > > >名称错误:未定义名称“Y” --------------------------------------------------------------------------- 我试过以下方法 >怀孕=输入(“你怀孕了吗?”) >怀孕=输入.upper(“你怀孕了(Y)es还是(N):”) >怀孕=输入(“你怀孕了(Y)es还是(N):”。上限() >怀孕的=怀孕的 -------------------------------------

Input 我无法将输入定义为Y(是),我收到回溯调用 这是一个作业 当涉及到怀孕是否怀孕的问题时=Y如果不是N 它把我扔回去了Y没有定义?因为它区分大小写。 --------------------------------------------------------------------------- >名称错误 >回溯(最近一次呼叫最后一次) >在 >16如果年龄>18岁,性别=“F”: >17怀孕=输入(“你怀孕了(Y)es还是(N):”)。上限() >--->18如果怀孕==Y.upper(): >19打印(“你需要一张医生的便条,你可以飞:”) > > >名称错误:未定义名称“Y” --------------------------------------------------------------------------- 我试过以下方法 >怀孕=输入(“你怀孕了吗?”) >怀孕=输入.upper(“你怀孕了(Y)es还是(N):”) >怀孕=输入(“你怀孕了(Y)es还是(N):”。上限() >怀孕的=怀孕的 -------------------------------------,input,python-3.6,Input,Python 3.6,问题是确定旅行者是否可以按原样出行或是否需要额外的文件——通过输入一个是或否的问题,并返回一个“OK you good to go”,或需要更多的信息和文件。第18行的问题是“怀孕”包含“Y”或“N”字符格式。因此,您需要说怀孕=='Y'.upper() 因此,当您比较if条件中的输入时,应如下所示: This is for an assignment when it gets to the Question about pregnant if pregnant = Y if not is N

问题是确定旅行者是否可以按原样出行或是否需要额外的文件——通过输入一个是或否的问题,并返回一个“OK you good to go”,或需要更多的信息和文件。第18行的问题是“怀孕”包含“Y”或“N”字符格式。因此,您需要说怀孕=='Y'.upper()

因此,当您比较if条件中的输入时,应如下所示:

This is for an assignment

when it gets to the Question about pregnant if pregnant = Y if not is N
it throws me back Y is not defined ? because its case-sensitive.
---------------------------------------------------------------------------
>NameError                                 
>Traceback (most recent call last)
><ipython-input-1-8e4818ff5970> in <module>
>     16 elif Age > 18 and Sex == "F":
>     17     pregnant = input("Are you pregnant (Y)es or (N): ").upper()
>---> 18     if pregnant == Y.upper():
>     19         print("You need a physician's note that you can fly: ")
>  
>
>NameError: name 'Y' is not defined

---------------------------------------------------------------------------


I've tried the following

>pregnant = input("Are you pregnant (Y)es or (N): ")
>pregnant = input.upper("Are you pregnant (Y)es or (N): "))
>pregnant = input("Are you pregnant (Y)es or (N): ").upper()
>pregnant = pregnant.upper()



-------------------------------------
我已经测试过了,效果很好:

pregnant = input("Are you pregnant (Y)es or (N)o: ").upper()
if pregnant == 'Y'.upper():
    print("You need a physician's note that you can fly: ")

第18行的问题是,“怀孕”以字符格式保存“Y”或“N”。因此,您需要说怀孕=='Y'.upper()

因此,当您比较if条件中的输入时,应如下所示:

This is for an assignment

when it gets to the Question about pregnant if pregnant = Y if not is N
it throws me back Y is not defined ? because its case-sensitive.
---------------------------------------------------------------------------
>NameError                                 
>Traceback (most recent call last)
><ipython-input-1-8e4818ff5970> in <module>
>     16 elif Age > 18 and Sex == "F":
>     17     pregnant = input("Are you pregnant (Y)es or (N): ").upper()
>---> 18     if pregnant == Y.upper():
>     19         print("You need a physician's note that you can fly: ")
>  
>
>NameError: name 'Y' is not defined

---------------------------------------------------------------------------


I've tried the following

>pregnant = input("Are you pregnant (Y)es or (N): ")
>pregnant = input.upper("Are you pregnant (Y)es or (N): "))
>pregnant = input("Are you pregnant (Y)es or (N): ").upper()
>pregnant = pregnant.upper()



-------------------------------------
我已经测试过了,效果很好:

pregnant = input("Are you pregnant (Y)es or (N)o: ").upper()
if pregnant == 'Y'.upper():
    print("You need a physician's note that you can fly: ")

input
将存储用户输入(y,y,n,n,zqy…),您可以将其转换为大写,但您想将其与字符串进行比较
“y”
感谢各位的快速响应-有什么方法可以详细介绍一下吗。如何修复它因为问题(M)ale或(F)emale没有回溯,并且它的构造方式非常相同,但它接受了M或F
输入
将存储用户输入(y,y,n,n,zqy…),您可以将其转换为大写,但您希望将其与字符串
“y”进行比较
谢谢你们的快速响应——有没有办法让你们详细介绍一下呢。如何修复它,因为问题(M)ale或(F)emale没有回溯,并且它是以相同的方式构造的,但它接受了M或F