2016-05-12 14 views
-2

我對這個網站很陌生,所以請耐心等待我在網站上提出的問題。我需要一些幫助來完成這個基於微型文本的遊戲,作爲學習python的硬道教書籍。這是我寫的代碼。我不知道缺失的鏈接和我做錯了什麼。幫助將不勝感激!建立一個迷你的基於文本的python遊戲作爲學習python的難題的作業

從SYS進口出口

高清啓動(): 打印 「你是在一箇舊廟」。 print「你的左右有一扇門,或者你可以走路。」 打印「你拿哪一個?」

choice = raw_input("> ") 

if choice == "left": 
    gold_room() 
elif choice == "right": 
    trap_room() 
elif choice == "forward": 
    monster_room() 
else: 
    dead("you got caught by the ancient gods and you must be killed.") 

的start()

高清monster_room(): 打印 「?你在與怪物的房間你該怎麼辦」

choice = raw_input("> ") 
if "left" in choice: 
print "you are going to the gold room" 
gold_room() 
elif "right" in choice: 
print "you are going to the trap room" 
trap_room() 

else: 
    dead("couldnt understand what did you say so you are dead!") 

高清gold_room(): 打印「您選擇左邊的房間,現在你是在用了第一桶金的房間!」 打印「你可以拿鍋。」 print「或者你可以把錢搶走。」打印「 」或者你去其他房間。「

choice = raw_input("> ") 

if choice == "take the pot": 
    print "you are a millionaire from now on!!!" 

elif choice == "rob the money": 
    dead("you will never rest in piece!") 

else choice == "another room": 
    monster_room() 

高清trap_room(): 打印 「你現在是一個陷阱房」 打印「這個房間裏有一個隱藏的陷阱。」 打印「小心!」 打印「你可以回去了怪物房」 打印「或者你可以找到陷阱」

choice = raw_input("> ") 

if "find" in choice: 
    start() 
elif "back" in choice: 
    gold_room() 

高清死了(爲什麼): 打印爲什麼「rekt!」 exit(0)

+0

什麼問題?你能指出一點嗎?代碼是否正在運行?或者拋出錯誤? – linusg

+0

@linusg好,我敢肯定,這段代碼有一個錯誤,像我這樣的初學者看不到,需要修改才能讓程序運行,但是要開始: elif choice ==「forward」: ^ SyntaxError:無效的語法 – eliwax

+0

那麼你會得到一個錯誤?你可以發佈回溯? – linusg

回答

1

好吧,我固定它。您的代碼有幾個縮進錯誤,在def聲明後Python需要四個空格或一個製表符縮進。

另一件事是,你用else條件測試(else choice == "another room":)。這是錯誤的,它應該是elif choice == "another room":或其他。

您也許已經注意到,我將raw_input()更改爲input()。這將所有輸入轉換爲字符串(input()將把整數解釋爲整數,將列表解釋爲列表等等),並且也更安全。

最後一件事是,在定義所有被調用的函數之前運行你的程序(start()),這是行不通的!

你的代碼(固定):

from sys import exit 

def start(): 
    print("You are in an old temple.") 
    print("There is a door to your right and left or you can walk forwad.") 
    print("Which one do you take?") 

    choice = input("> ") 

    if choice == "left": 
     gold_room() 
    elif choice == "right": 
     trap_room() 
    elif choice == "forward": 
     monster_room() 
    else: 
     dead("you got caught by the ancient gods and you must be killed.") 


def monster_room(): 
    print("you're in a room with a monster. what you gonna do?") 

    choice = input("> ") 
    if "left" in choice: 
     print("you are going to the gold room") 
     gold_room() 
    elif "right" in choice: 
     print("you are going to the trap room") 
     trap_room() 
    else: 
     dead("couldn't understand what did you say so you are dead!") 


def gold_room(): 
    print("you chose the left room. now you are in a room with a pot of gold!") 
    print("you can take the pot.") 
    print("or you can just rob the money in it.") 
    print("or you go go to other rooms.") 

    choice = input("> ") 

    if choice == "take the pot": 
     print("you are a millionaire from now on!!!") 
    elif choice == "rob the money": 
     dead("you will never rest in piece!") 
    elif choice == "another room": 
     monster_room() 


def trap_room(): 
    print("you are now in a trap room.") 
    print("there is a hidden trap in this room.") 
    print("be careful!") 
    print("you can go back to the monster room") 
    print("or you can find the trap") 

    choice = input("> ") 

    if "find" in choice: 
     start() 
    elif "back" in choice: 
     gold_room() 


def dead(why): 
    print(why, "rekt!") 
    exit(0) 

start() 
1

我會在所有你定義的函數之後調用start()函數。編寫一個通常的方法是在最後寫了下面的代碼:

if __name__ == "__main__": 
    start() 

這基本上意味着PROGRAMM運行在最後的start()函數,如果你執行文件。

此外,您必須在定義函數後留出空格。您寫道:

def monster_room(): 
print "you're in a room with a monster. what you gonna do?" 

,但它應該是:

def monster_room(): 
    print "you're in a room with a monster. what you gonna do?" 

如果沒有幫助,指出問題

+0

我編輯的代碼,你看到代碼本身的任何其他問題? – eliwax

+0

linugs回答下方已經修復它 – sorh

+0

除了他使用打印(「字符串),因爲他似乎在使用python 3.如果這不適合你,嘗試打印」字符串「,而不是使用python 2.X – sorh