用户在Python中输入保留关键字。发生了一个

2024-09-29 23:15:15 发布

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

我正在做一个简单的程序(只是开个玩笑)。程序需要用户输入yes或no(可以是不同的语言)。但当他输入一个保留字(i。e。关键字)发生错误是因为该关键字在代码中存在一些错误

我的截断代码(可能因为被截断而显得不清楚):

x = input('Enter yes or no (you can do this in different languages...) ')
x = x.lower()
answersYes = ['yes','si','oui','ioe','inde','tak','ja','da']
answersNo = ['no','ayi','che','leai','nie','ne','nein']
if ' ' in x:
    print('Input just one word!')
else:
    if x in answersYes:
        print('You enteres YES!')
    elif x in answersNo:
        print('You enteres NO!')
    else:
        print('Sorry, but this isn\'t YES nor NO!')

我在谷歌上搜索了一下,但还没找到运气

谢谢你的回答

附言

只是一个小提示:

当我在basic Python IDLE中用Python运行上层脚本时,没有任何错误,但是当我在Spyder中运行此脚本时,显示了以下消息(当我在no中键入'yes'(“in”是一个保留字)):

 File "<ipython-input-49-d1e48c3ddecb>", line 1
    yes in no
        ^
SyntaxError: invalid syntax

Tags: no代码in程序inputif错误关键字

热门问题