2012-07-04 16 views
0

我想做一個簡單的遊戲。簡單的Python腳本中的流控制

邏輯是這樣的:「有五個門,每個門編號爲1到5,用戶將被要求輸入任意一個數字,例如,如果他們輸入」1「,GoldRoom將被打開(並且相關的類將被處理)。「

現在,我已經定義了一個類GoldRoom(),並進行了測試,輸入了「1」。處理過程按預期進行。但是,當我輸入「2」作爲我的選擇時,處理仍然發生,而不是print語句,即else語句沒有執行。

我哪裏錯了?

################################# 
# Learning to make a game# 
################################# 

# An attempt to make a game 
# Each room will be described by a class, whose base class will be Room 
# The user will be prompted to enter a number, each number will be assigned with a Room in return 

from sys import exit 

print "Enter your choice:" 
room_chosen = int(raw_input("> ")) 

if room_chosen == 1: 
    goldroom = GoldRoom() 
    goldroom.gold_room() 


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

#class Room(object): #the other room will be derived of this 
# pass 

class Room(object): 
    pass 

class GoldRoom(Room): 

    # here the user will be asked with question on how much Gold he wants 

    print"This room is full of gold. How much do you take!" 

    next = raw_input("> ") 

    if "0" in next or "1" in next: 
     how_much = int(next) 
     print how_much 
    else: 
     dead("Man, learn to type some number") 

    if how_much < 50: 
     print "Nice, you are not greedy, you win!" 
     exit(0) 

    else: 
     dead("You greedy bastard!") 

#class KoiPondRoom(Room): 

    # in this room, the user will be made to relax 

#class Cthulhu_Room(Room): 

    # sort of puzzle to get out 

#class Bear_Room(Room): 

    # bear room 

#class Dark_Room(Room): 

    # Dark Room, will be turned into Zombie 

#class Dead_Room(Room): 

    # Those who enter here would be dead 
if room_chosen == 1: 
    goldroom = GoldRoom() 
    goldroom.gold_room() 
else: 
    print "YOU SUCK!" 

回答

5

的問題是在這裏:

class GoldRoom(Room): 

    # here the user will be asked with question on how much Gold he wants 

    print"This room is full of gold. How much do you take!" 

與裝入蟒蛇VM的整個源代碼,執行這段代碼,並將其打印一些東西,你應該將其更改爲:

class GoldRoom(Room): 

    # here the user will be asked with question on how much Gold he wants 
    def gold_room(self): 
     print"This room is full of gold. How much do you take!" 

     next = raw_input("> ") 

     if "0" in next or "1" in next: 
      how_much = int(next) 
      print how_much 
     else: 
      dead("Man, learn to type some number") 

     if how_much < 50: 
      print "Nice, you are not greedy, you win!" 
      exit(0) 

     else: 
      dead("You greedy bastard!") 
+0

哦,好的,謝謝你的幫助:) ..我錯過了那個班,但初始化它:) – peterparker

+0

但現在我遇到了一個錯誤: – peterparker

+0

PS C:\ USers \ rk \ lpthw> python ex45.py在 類戈爾德魯姆(室) 文件 「ex45.py」 第24行:輸入您的選擇: > 1 回溯(最近通話最後一個) 文件 「ex45.py」 32行,在戈爾德魯姆 如果在接下來的 「0」 或在下一個 「1」: 類型錯誤:類型 'builtin_function_or_method' 的參數不是可迭代 – peterparker