2014-02-22 90 views
0

我正在python 3.3.4中製作一個非常簡單的文本冒險遊戲,並想知道是否有任何可能的方法來使它當輸入不匹配a/b它返回到打印命令,詢問問題。Python文本冒險循環

這是我目前有:

import time 
import sys 
from random import randrange 

text = "** Execute Long Intro **" 

for c in text: 
    sys.stdout.write(c) 
    sys.stdout.flush() 
    seconds = "0." + str(randrange(1, 2, 1)) 
    seconds = float(seconds) 
    time.sleep(seconds) 


time.sleep(1) 


obj1 =input('\nDo you \na.) Rest on the ground \nb.) Find a way out of the jungle\n') 
if obj1 in ('a'): 
    print('You find a comfortable spot on the ground and drift into sleep...') 
    time.sleep(.6) 
    print('Zzz.\nZzz..\nZzz...') 
    time.sleep(3) 
    print('You wake to a strange noise, and work your way out of the jungle.') 
    time.sleep(1) 

    print('You emerge out of the jungle and walk along the shoreline of a sunny beach') 
    time.sleep(1) 
    print('** Objective One Completed **') 
    time.sleep(2) 



elif obj1 == 'b': 
    print('You manage to find a path out of the jungle and discover a beach,') 
    time.sleep(1) 
    print('** Objective One Completed **') 
    time.sleep(2) 

elif obj1 != ('a','b'): 
    print('Uh.') 

幫助將不勝感激。

回答

1

使用while循環:

obj1 = input(...) 

while obj1 not in ('a', 'b'): 
    obj1 = input("Invalid. Enter again: ") 

if obj1 == 'a': 
    ... 
elif obj1 == 'b': 
    ... 
0

您可以嘗試使用blessings庫。 與安裝:

pip install blessings 

和用法很簡單:

from blessings import Terminal 
term = Terminal() 
print term.clear_bol 
print '\t{t.yellow}action{t.normal}|{t.green}Done!{t.normal}'.format(t=term) 
+0

我安裝的祝福伴點子,當我的侏儒您提供的代碼得到一個無效的語法錯誤。 – dawsondiaz

+0

你明白了。簡單地修復錯誤。在祝福頁面閱讀文檔。我沒有看到代碼中的任何語法錯誤! – Farsheed