2014-10-02 115 views
-1

的Python 2.7

Windows 7的爲什麼不循環?

當我鍵入 '發揮' 一次:

if(game == "play"): 
print("Make sure you type 'Play', CaPS MaTteR! ") 
startg() 
elif(game == "Play"): 
playg() 

它做什麼是應該做的。當我輸入''錯誤兩次,而不是循環它只是退出。搜索它在谷歌,但找不到任何東西:/

代碼:

name = raw_input("What is your name? ") 
gender = raw_input("What are you, a Boy or a Girl? ") 
print(" ") 

if(gender == "Boy"): 
their = "his " 
else: 
their = "her " 

game = raw_input("Type 'Play' to start. ") 

def endg(): 
print("Hope you had fun! ~Red") 

def startg(): 
game = raw_input("Type 'Play' to start. ") 
if(game == "Play"): 
print("Loading. . . ") 
playg() 

listq1 = ["A. Quit your job." , "B. Pretend you never saw the stack of papers." , "C.    Kill yourself because you don't feel like playing this game. "] 

def playg(): 
answer = raw_input("You are a programmer, " + name + ", who hates " + their + "job  very much." + 
     " You walk into work to see a huge stack of papers on your desk... What do you  do? \n" + "\n".join(listq1)) 
if(answer == "A"): 
    print("\nYou look around the room and see the flock of miserable people...Your  Co-Workers. Is working here really worth the stress? ") 
elif(answer == "B"): 
    print("\nYou pull the over-sized recycle bin out from under your desk. Just as you start to slide the papers to their impending doom, a fellow co-worker stops to ask what you are doing. ") 
elif(answer == "C"): 
    endg() 

if(game == "play"): 
print("Make sure you type 'Play', CaPS MaTteR! ") 
startg() 
elif(game == "Play"): 
playg() 

回答

2

看看你的代碼:

def startg(): 
game = raw_input("Type 'Play' to start. ") 
if(game == "Play"): 
print("Loading. . . ") 
playg() 

if(game == "play"): 
print("Make sure you type 'Play', CaPS MaTteR! ") 
startg() 
elif(game == "Play"): 
playg() 

,如果你敲錯「播放」發生,因爲什麼「玩」第一次?那麼,它會去startg()。有條件if(game=="Play"),但沒有其他語句來處理任何事情,但正確的輸入。所以如果你第二次輸入「play」,這會導致程序沒有考慮到的情況。所以沒有函數被調用,並且你的程序進行到最後。

順便說一句,你應該引入else語句來處理所有案件中,「播放」以其它方式拼寫錯誤。不正確的大寫並不是您應該準備處理的唯一潛在錯誤。

+0

我知道這會很簡單,謝謝! C: – 2014-10-02 03:28:41