2016-11-15 79 views
0

我正在製作一個文本冒險遊戲,我試圖讓用戶使用q退出遊戲。我不知道該輸入什麼,這是我的代碼。如何讓用戶退出程序

print ("Welcome to Camel!") 
print ("You have stolen a camel to make your way across the great Mobi deset.") 
print ("The natives want their camel back and are chasing you down! Survive your desert trek and out run the natives.") 

print ("Welcome to Camel!") 
print ("You have stolen a camel to make your way across the great Mobi deset.") 
print ("The natives want their camel back and are chasing you down! Survive your desert trek and out run the natives.") 

done = False 
while done == False: 
    print("Print the letter only.") 
    print("A. Drink from your canteen.") 
    print("B. Ahead moderate speed.") 
    print("C. Ahead full speed.") 
    print("D. Stop for the night.") 
    print("E. Status check.") 
    print("Q. Quit.") 

    first_question = input("What do you want to do? ") 

    if first_question.lower() == "q": 
     done = True 

    if done == True: 
     quit 

所有的行都在代碼中正確縮進(當複製粘貼時,網站變得很奇怪)。任何幫助表示讚賞。

+1

我把你的代碼編輯成一個代碼塊。將來,您可以突出顯示代碼部分,然後單擊帶花括號的按鈕{}'以縮進代碼 – James

+0

請參閱https://stackoverflow.com/questions/543309/programatically-stop-execution-of- python-script – AnilRedshift

+0

什麼是'退出'?也許你是用'()'來表示'break'或'quit()'。但刪除'如果完成== True:退出',它應該按照您的預期工作。 – furas

回答

-2
print ("Welcome to Camel!") 
print ("You have stolen a camel to make your way across the great Mobi deset.") 

print ("The natives want their camel back and are chasing you down! Survive your desert trek and out run the natives.") 

done = False 

while done == False: 

    print("Print the letter only.") 

    print("A. Drink from your canteen.") 

    print("B. Ahead moderate speed.") 

    print("C. Ahead full speed.") 

    print("D. Stop for the night.") 

    print("E. Status check.") 

    print("Q. Quit.") 

    first_question = input("What do you want to do? ") 

    if first_question.lower() == "q": 

     done = True 

     if done == True: 

      break 

現在,當您輸入'Q'或'q'程序將退出。 這是關於製表。這是你問的嗎?

+0

[quit](https://docs.python.org/2/library/constants.html#quit)是一種退出的內置方法 – Hamms

+0

據我所知,我們使用'break'來斷開循環,女巫我在這裏看不到。所以'退出'會做,它的作品。 –

+0

@leaf我明白了。所以當我們使用while /如果break是停止的正確方法。但退出也會在這裏工作? –

0

你的程序幾乎沒有問題,事實上它在python3中運行的很好。問題是,在python2中,input實際上是評估您的輸入。爲了支持python2,使用raw_input

相關問題