我正在写俄勒冈州小径游戏,这是我的代码,这是造成问题,我不知道为什么会有问题。我想做的是,如果他们输入一个包含列表中某个单词的名称,则会将变量easter_mode设置为1,如果他们不输入,则会将easter_mode设置为0。需要在列表中列出的单词是:(Sturtz,Sturtz,Nate,Nate) 多谢各位
#asking name
player_name = input('What is your name:')
while len(player_name) >= 0:
if len(player_name) > 1:
print("Weclome" + str(player_name))
print('Which mode do you want to play?')
mode_choice = input('(easy) More modes comming soon:')
break
if len(player_name) == 1:
player_name_choice = input(str(player_name)+"? Are you kidding me? Only one letter? You might regreat it (Y/N):")
if player_name_choice == "y" or player_name_choice == "Y":
print("Ok Your Choice!!...")
mode_choice = 'easter'
break
if player_name_choice == "n" or player_name_choice == "N":
player_name = input('What is your name:')
else:
print("You do not type anything, try again")
player_name = input('What is your name:')
#Check Easter Egg Names
easter_names = ["nate sturtz", "Nate Sturtz", "Nate", "nate", "Sturtz", "sturtz"]
if player_name in easter_names:
easter_mode = 1
else:
easter_mode = 0
#easter eggs for name
if easter_mode == 1:
year_set = 2005
mode_choice = 'easter'
else:
year_set = input('Enter a year whatever you like:')
if year_set.isdigit():
return_num = 0
else:
return_num = 1
while return_num == 1:
print('Error,please try again!')
year_set = input('Enter a year whatever you like:')
if year_set.isdigit():
return_num = 0
else:
return_num = 1
year_set = int(year_set)
当我运行完整文件时,我得到
Traceback (most recent call last):
File "Oregon.py", line 64, in <module>
player_name = input('What is your name:')
File "<string>", line 1, in <module>
NameError: name 'nate' is not defined
您可以在Github上查看完整代码
https://raw.githubusercontent.com/nsturtz/Oregon-Trail/master/Oregon.py
在Python2中会出现此错误。在Python2中,
input()
在输入值时使用精确的值在您的示例中,您键入的是
nate
,而不是'nate'
。前一个值是一个变量名(在代码中没有定义,因此是NameError
),而后一个值是一个字符串在Python3中,
input()
的行为与您假设的一样,并将字符串传递给您的代码如果确定要使用Python 2,可以用
raw_input()
替换input()
,它将把输入解释为字符串而不是变量名在Python2下,可以使用
raw_input
而不是input
来防止Python将用户输入解释为Python代码但是,由于Python2已被弃用,我强烈建议不要使用它。改用Python 3,其中
input
按预期工作1当然除了维护传统产品。但这在这里似乎并不重要
相关问题 更多 >
编程相关推荐