我翻译一个孩子选择自己的冒险书成python程序例如,“如果你选择X转到页y或如果你选择了转到页面B”减少多个if语句表
虽然这个程序的作品,在本书的最后将会有超过100个if语句是否有任何方法来创建一个表,将用户输入与一个页面列表进行比较。同时研究我看到的例子显示了类似的表如下:
#this would hold the potential user inputs
[0,0,0,0,0,0]
[0,0,0,0,0,0]
[0,0,0,0,0,0]
但是我不能确定如何实现它
#imports the pages of the book from another python file
from Content import *
clrscrn = (chr(27) + "[2J")
def page0():
print "\n %s" % page1
page0()
#User input loop
while True:
inp = raw_input(">>> ").lower()
#clears the screen then prints the variable (page)
if inp == '3':
print clrscrn
print '%s' % page3
if inp == '10':
print clrscrn
print '%s' % page10
if inp == '5':
print clrscrn
print '%s' % page5
if inp == '14':
print clrscrn
print '%s' % page14
#quits python
elif inp == 'quit':
raise SystemExit
我试图实现这与当前的代码,但我不知道在哪里添加全局变量 –
解决它谢谢你!我删除了输入行下面的所有内容,并添加了您建议的内容,效果很好! –