简单Python脚本中的流控制

简单Python脚本中的流控制,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我想做一个简单的游戏 逻辑是这样的:“有五扇门,每个门的编号为1到5。用户将被要求输入任何一个数字。例如,如果他们输入“1”,金库将被打开(并且相关的类将被处理)。” 现在,我已经定义了一个类,GoldRoom(),对于测试,输入“1”。处理按预期进行。但是,当我选择输入“2”时,仍会进行处理,而不是打印语句,即不会执行else语句 我哪里做错了 ################################# # Learning to make a game# ############

我想做一个简单的游戏

逻辑是这样的:“有五扇门,每个门的编号为1到5。用户将被要求输入任何一个数字。例如,如果他们输入“1”,金库将被打开(并且相关的类将被处理)。”

现在,我已经定义了一个类,
GoldRoom()
,对于测试,输入“1”。处理按预期进行。但是,当我选择输入“2”时,仍会进行处理,而不是打印语句,即不会执行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!"
#################################
#学习制作游戏#
#################################
#做游戏的尝试
#每个房间都将由一个类来描述,其基类为room
#系统将提示用户输入一个号码,每个号码将分配一个房间作为回报
从系统导入退出
打印“输入您的选择:”
所选房间=整数(原始输入(“>”)
如果选择的房间=1:
goldroom=goldroom()
金屋
def死亡(原因):
打印“为什么,干得好!”
出口(0)
#教室(对象):#另一个教室将由此派生
#通过
教室(实物):
通过
班级会议室(房间):
#在这里,用户将被问及他想要多少黄金
打印“这间屋子里全是金子,你要多少钱?”
下一步=原始输入(“>”)
如果下一个中的“0”或下一个中的“1”:
多少=整数(下一个)
打印多少钱
其他:
死亡(“人,学打数字”)
如果多少小于50:
打印“很好,你不贪婪,你赢了!”
出口(0)
其他:
死了(“你这个贪婪的混蛋!”)
#KoiPondRoom(房间)等级:
#在这个房间里,使用者可以放松
#Cthulhu_房间(房间)等级:
#有点让人困惑
#班长会议室(会议室):
#熊室
#班级暗室(房间):
#黑暗的房间,将变成僵尸
#班级休息室(房间):
#进入这里的人都会死
如果选择的房间=1:
goldroom=goldroom()
金屋
其他:
打印“你真烂!”
问题在于:

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!"
当整个源代码加载到python 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!")
class黄金室(房间):
#在这里,用户将被问及他想要多少黄金
def gold_房间(自):
打印“这间屋子里全是金子,你要多少钱?”
下一步=原始输入(“>”)
如果下一个中的“0”或下一个中的“1”:
多少=整数(下一个)
打印多少钱
其他:
死亡(“人,学打数字”)
如果多少小于50:
打印“很好,你不贪婪,你赢了!”
出口(0)
其他:
死了(“你这个贪婪的混蛋!”)
问题在于:

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!"
当整个源代码加载到python 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!")
class黄金室(房间):
#在这里,用户将被问及他想要多少黄金
def gold_房间(自):
打印“这间屋子里全是金子,你要多少钱?”
下一步=原始输入(“>”)
如果下一个中的“0”或下一个中的“1”:
多少=整数(下一个)
打印多少钱
其他:
死亡(“人,学打数字”)
如果多少小于50:
打印“很好,你不贪婪,你赢了!”
出口(0)
其他:
死了(“你这个贪婪的混蛋!”)

oh ok,谢谢您的帮助:)…我错过了类中的def,但初始化了它:)PS C:\USers\rk\lpthw>python ex45.py输入您的选择:>1回溯(最近一次调用):文件“ex45.py”,第24行,在类GoldRoom(Room)中:文件“ex45.py”,第32行,在GoldRoom中,如果下一次调用“0”或“1”在next:TypeError:type'builtin_function_或_method'的参数不是iterableclass GoldRoom(Room):def gold_Room(self):#在这里,用户将被问到他想要多少金子打印“这个房间里装满了金子。你要拿多少钱!”next=raw_输入(“>”),如果next中的“0”或next中的“1”:how_much=int(next)else:dead(“伙计,学着键入一些数字”)如果多少钱<50:print“不错,你不贪婪,你赢了!”退出(0)else:dead(“你这个贪婪的混蛋!”)@peterparker change>>>如果下一个“0”或下一个“1”:>>如果下一个.isdigit()“PS C:\USers\rk\lpthw>python ex45.py输入你的选择:>0回溯(最近一次调用):文件“ex45.py”,第24行,类内GoldRoom(Room):文件“ex45.py”,第32行,在GoldRoom中如果next.isdigit():AttributeError:“内置函数”或“方法”对象没有属性“isdigit”PS C:\USers\rk\lpthw>python ex45.py输入您的选择:>1回溯(最后一次调用):文件“ex45.py”,第24行,类内GoldRoom(Room):文件“ex45.py”,第32行,在GoldRoom if next.isdigit():AttributeError:“内置函数”或“方法”对象没有属性“isdigit”哦,好的,谢谢帮助:)…我在类中错过了该定义,但正在初始化它:)PS C:\USers\rk\lpthw>python ex45.py输入您的选择:>1回溯(最后一次调用):File“ex45.py,第24行,类内GoldRoom(Room):文件“ex45.py”,第32行,如果next中的“0”或next中的“1”:TypeError:type'builtin_function_或_method'的参数不是iterableclass GoldRoom(Room):def gold_Room(self):#这里将询问用户想要打印多少黄金“这间屋子里装满了金子。你要多少钱!“next=raw\u input(“>”)如果next中的“0”或next中的“1”:how\u much=int(next)否则:dead(“伙计,学会键入一些数字”)如果how\u much<50:print“很好,你不贪婪,你赢了!“exit(0)else:dead(“你这个贪婪的混蛋!”)@peterparker change>>>如果next中的“0”或next中的“1”:>>如果next.isdigit()”PS C:\USers\rk\lpthw>python ex45.py输入你的选择:>0回溯(最近一次调用):文件“ex45.py”,第24行,在课堂黄金室(Roo