当我尝试做几个分支时,它总是给我一个名称错误

2024-09-25 10:18:49 发布

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

我正在尝试创建几个分支,这一直给我这个名称错误

我试着删除它给我带来的问题,然后我想我是在删除整件事

while True:
    d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
    # check if d1a is equal to one of the strings, specified in the list
    if d1a in ['A', 'B']:
        # if it was equal - break from the while loop
        break
if d1a == "A": 
    print ("You approach the cottage.") 
elif d1a == "B": 
    print ("You approach the stables.")

错误:回溯(最近一次呼叫上次): 文件“main.py”,第5行,在 d1a=输入(“你想:A)接近房子。B) 接近马厩[A/B]?:”) 文件“”,第1行,在 NameError:未定义名称“A”


Tags: 文件thetoin名称youif错误
1条回答
网友
1楼 · 发布于 2024-09-25 10:18:49

看起来您正在运行Python 2,也称为遗留Python。在该版本的Python中,input尝试评估输入。您输入了A,因此Python试图找到它的值。没有名为A的变量,因此您收到了错误消息

raw_input代替input。或者更好的是,转到Python3。您的代码在我的Python3.7.3中运行良好

相关问题 更多 >