Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据python中的输入循环基于文本的游戏_Python_Loops_Text - Fatal编程技术网

如何根据python中的输入循环基于文本的游戏

如何根据python中的输入循环基于文本的游戏,python,loops,text,Python,Loops,Text,到目前为止,我有: print('a skeleton comes into view, the hiker must have been dehydrated.') print ('he was wearing a Yankees HAT, to the right of his body he set his BACKPACK and WOODEN WALKING STICK next to the wall') input2 = raw_input ("You may SE

到目前为止,我有:

print('a skeleton comes into view, the hiker must have been dehydrated.')
print ('he was wearing a Yankees HAT, to the right of his body he set his BACKPACK         and WOODEN WALKING STICK next to the wall')
input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ")
if input2 == 'PICK UP HAT':
    print 'taken'
    hat = hat+1
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ")
#
#
#

if input2 == 'SEARCH BACKPACK':
    print ("there are OLD CLOTHES in here as well as a TARP")
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ")


elif input2 == 'PICK UP CLOTHES':
    print ("tsken")
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ")


elif input2 == 'PICK UP TARP':
    print ("taken")
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ")


elif input2 == 'PICK UP BONE':
    print ("taken")
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ")


elif input2 == 'PICK UP WOODEN WALKING STICK':
    print "Taken"
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ")


elif input2 == 'GO ON':
    input3 = raw_input ("left or right: ")
    if input3 == 'left':
        import module3
    elif input3 == 'right':
        import module4
我很难理解我是否应该在这里创建一个while或for语句


例如:如何使玩游戏的人在不搜索背包的情况下不能两次拿起帽子或拿起防水油布。

解决部分问题的方法是使用调度器:

def pick_up_hat():
  return True # do stuff

def search_backpack():
  return False # do stuff

actions = {
  'PICK UP HAT': pick_up_hat,
  'SEARCH BACKPACK': search_backpack,
  # ...
}

go = True
while go:
  cmd = raw_input().strip()
  go = actions[cmd]()

请注意,您的设计中还有一些其他问题需要解决,例如管理状态。

我建议使用cmd模块,它是标准库的一部分。它为实现基于终端的命令解析、菜单等提供了方便的基础设施。以下是一个很好的基础教程:

还有一个名为cmd2的插入式替换第三方模块,其工作方式相同,但具有更多功能:


关于SO的未来帖子,您可能想看看。我尽可能地清理了你的代码缩进。