用户在 Python 中输入保留关键字。发生错误
User inputs reserved keyword in Python. There happens an error
我正在开发一个简单的程序(开个玩笑)。程序希望用户输入是或否(可以使用不同的语言)。但是当他输入保留字(即关键字)时发生错误,因为该关键字在代码中存在一些错误。
我的 t运行cated 代码(可能看起来不清楚,因为它是 t运行cated):
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!')
我用谷歌搜索了一下,但还没有成功。
非常感谢您的回答!
P.S.
只是一张小纸条:
当我在基本 Python IDLE 中有 运行 上面的脚本 Python 时,没有任何错误,但是当我在 Spyder 中有 运行 this 时, 那里显示了这个消息(当我输入 'yes in no' ("in" 是一个保留字):
File "<ipython-input-49-d1e48c3ddecb>", line 1
yes in no
^
SyntaxError: invalid syntax
我没有收到错误。确保你的 spyder 是 python3-configured 或尝试 运行 python3 file.py
您也可以尝试将 input()
替换为 raw_input()
我正在开发一个简单的程序(开个玩笑)。程序希望用户输入是或否(可以使用不同的语言)。但是当他输入保留字(即关键字)时发生错误,因为该关键字在代码中存在一些错误。
我的 t运行cated 代码(可能看起来不清楚,因为它是 t运行cated):
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!')
我用谷歌搜索了一下,但还没有成功。
非常感谢您的回答!
P.S.
只是一张小纸条:
当我在基本 Python IDLE 中有 运行 上面的脚本 Python 时,没有任何错误,但是当我在 Spyder 中有 运行 this 时, 那里显示了这个消息(当我输入 'yes in no' ("in" 是一个保留字):
File "<ipython-input-49-d1e48c3ddecb>", line 1
yes in no
^
SyntaxError: invalid syntax
我没有收到错误。确保你的 spyder 是 python3-configured 或尝试 运行 python3 file.py
您也可以尝试将 input()
替换为 raw_input()