我翻譯一個孩子選擇自己的冒險書成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
我試圖實現這與當前的代碼,但我不知道在哪裏添加全局變量 –
解決它謝謝你!我刪除了輸入行下面的所有內容,並添加了您建議的內容,效果很好! –