0

我正在尝试制作一个简单的游戏。

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

现在,我定义了一个类,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!"
4

1 回答 1

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!"

当整个源代码加载到 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!")
于 2012-07-04T07:42:42.720 回答