2015-11-19 35 views
-2

我在業餘時間自學python。我只在這裏待了幾天,所以在練習中我一直在使用各種raw_input線。目前,我正在嘗試爲朋友嘗試一個簡短的文字冒險。我打了一個街區,其中一部分詢問用戶是否想進村。如果他們選擇進入,一切都會正常工作,但如果他們選擇不這樣做,我需要它跳過村子內部發生的所有對話和投入。如何讓它跳轉到頁面下方的輸入?python中的分支對話

這是很基本的,但在這裏:

name = raw_input("What is your name adventurer? ") 
where = raw_input("You are in a magical forest, you can go North or East, which 
way do you go?") 
if where.lower() == "north": 
    troll = raw_input("You encounter a troll, what do you do? Attack or flee? ") 
else: 
    print("You encounter a dragon, you are dead, GAME OVER") 
if troll.lower() == "attack": 
    print ("You have no weapon, that was a bad choice, you are dead, GAME OVER") 
else: 
    flee = raw_input("You flee further North, you encounter a village, do you 
enter or continue? ") 
if flee.lower() == "enter": 
    army = raw_input("As you enter the village, a knight hands you a sword, 
assuming you are there to join the army. Join him or leave the village? ") 
else: 
    print ("You ignore the village and continue down the road.") #jump from here 
if army.lower() == "join": 
    print ("You head off to war and are killed almost instantly because you are an 
untrained fool") 
else: 
    print ("You are kicked out of the village") 
dark = raw_input("It is getting dark, do you continue or seek shelter") #to here 
if dark.lower() == "continue": 
    print ("You are attacked by vampires and killed, GAME OVER") 
else: 
    caves = raw_input("You find two caves, do you pick cave one which is pitch 
black, or two which has light coming from it? ") 
if caves.lower() == "one": 
    print ("You enter the cave, light a small fire, and sleep for the night.") 
else: 
    print ("You enter the cave, there are bandits inside, you are murdered, GAME OVER") 

我想第一粗線後跳下來。如果發現任何可以修復或改善的東西,請說出來。所有的批評都是受歡迎的。

+0

請[edit](http://stackoverflow.com/posts/33811111/edit)你的問題和[格式化代碼](http://meta.stackoverflow.com/a/251362/1555990)。 – That1Guy

+0

FWIW,格式化的部分原因是我們可以將您的完整代碼剪切並粘貼到編輯器中並自行運行。 –

回答

0

變化塊:

if flee.lower() == "enter": 
    army = raw_input("As you enter the village, a knight hands you a sword, 
assuming you are there to join the army. Join him or leave the village? ") 
else: 
    print ("You ignore the village and continue down the road.") #jump from here 
if army.lower() == "join": 
    print ("You head off to war and are killed almost instantly because you are an 
untrained fool") 
else: 
    print ("You are kicked out of the village") 
dark = raw_input("It is getting dark, do you continue or seek shelter") 

成塊:

if flee.lower() == "enter": 
    army = raw_input("As you enter the village, a knight hands you a sword, 
assuming you are there to join the army. Join him or leave the village? ") 
    if army.lower() == "join": 
     print ("You head off to war and are killed almost instantly because you are an 
untrained fool") 
    else: 
     print ("You are kicked out of the village") 
else: 
    print ("You ignore the village and continue down the road.") 
dark = raw_input("It is getting dark, do you continue or seek shelter") 

這使army輸入enter塊內,這樣,如果去這句話裏面將只執行。

的幾個注意事項: 你可能想把這一切裏面main()功能和呼叫return每當你死,所以它不會繼續。如果你不想使用某個功能,另一種選擇是將所有內容都放入try區塊中,如果你死了,則調用raise,然後用except區塊處理死亡。你說你是一個初學者,所以你可能還沒有學過這個東西,但它肯定是要看的東西