我正在做一个选择你自己的冒险游戏,我有一个功能来检查你在控制台中输入的内容是否可以接受。在开始时,您只能键入“打开灯光”,如果您键入任何其他内容,它将返回错误,并提示您键入实际操作。我的问题是,在你键入一些不被接受的内容后,它将不会让你在出错后继续。你知道吗
actions = ['help','turn light on',]
def errorcheck(player_input):
if player_input in actions:
error = False
return()
else:
error = True
while error == True:
print('i dont know what you mean by',player_input)
player_input = input('>')
if player_input in actions:
error = False
else:
error = True
print('welcome to TITLE')
print('type help at anytime to see your options')
print('">" that symbol promts you to do something')
print('')
print('you wake up, its dark')
player_input = input('>')
errorcheck(player_input)
if error == False:
if player_input == ('help'):
playerhelp = True
while playerhelp == True:
print('you can: turn light on')
playerhelp = False
首先,绝对不要在主代码中使用函数中的局部变量。如果要访问
error
,应按如下方式返回:其次,也难怪程序在进入
help
之后会停止,因为在那之后就没有代码了。如果你想让用户不断地被要求输入一些东西,你必须在整个解析逻辑中加入一个循环。。。你知道吗errorcheck
可能修改它作为参数接受的player_input
。它是一个新的局部变量,与全局player_input
无关。你知道吗一个简单的解决方案是使
player_input
成为一个全局变量,但这将是一个糟糕的反模式解决方案,原因如下:相反,让
errorcheck
只检查输入的名称。你知道吗此时将
errorcheck
作为函数似乎有点多余。你并不真的需要它:相关问题 更多 >
编程相关推荐