0

开始时,如果一个随机整数 = 1,它将播放一个随机事件。应从包含每个场景名称的字典中选择(也随机)随机事件。相反,它不会尝试访问其他场景。它只按照编写的顺序运行每个方法(函数?)。我究竟做错了什么?这是解决这个问题的坏方法吗?

我还在学习,所以任何指针表示赞赏!

from random import randint
from time import sleep

class RandomEvent(object):

    def __init__(self, wallet):
        self.wallet = wallet

    def old_man(self):
        #insert art
        print "\nExcuse me, I have an unusual request."
        print "My horoscope tells me it's my lucky day,"
        print "but I don't have any money."
        print "\nIf you'll let me borrow some money, I"
        print "promise to give you half of all the winnings."
        print "Do we have a deal?"
        give_money = raw_input("(y)es or (n)o? ")
        if give_money.lower() == "y":
            print "\nFunds: $", self.wallet
            how_much = int(raw_input("How much money will you give? $"))
            if how_much <= self.wallet:
                how_much *= randint(2,3)
                print "Haha! I won $%d! Here's your share of the money, kiddo." % (how_much*2)
                self.wallet += how_much
        else:
            print "Eh, kids these days... no faith in their elders... (grumble grumble)..."
            sleep(2)
            print "\nA few moments later you see the old man hit the jackpot at a slot machine."

        return self.wallet

    def robber(self):
        #insert art
        print "\nPsssst! Hey, you!"
        #sleep(1)
        print "\nYou glance down to see a knife discreetly placed"
        print "to your stomach."
        #sleep(1)
        print "\nHand over all your cash. No funny business."
        print "\nFunds: $", self.wallet
        how_much = int(raw_input("How much money will you give? $"))
        lie_success = randint(1,3)
        if how_much == self.wallet:
            self.wallet = 0
            print "\nNice doin' business with ya."
            print "The robber quickly disappears into a nearby crowd,"
            print "taking all your money with him."

        if how_much != self.wallet and lie_success == 1:
            self.wallet -= how_much
            print "\nNice doin' business with ya."
            print "The robber quickly disappears into a nearby crowd,"
            print "taking your money with him."

        else:
            print "\nYou think you're a wise guy, eh?"
            sleep(2)
            print "\nYou are dead. GAME OVER"
            sleep(2)
            exit()

        return self.wallet


    def pretty_woman(self):
        pass


###----------------------GAME CODE BELOW---------------

funds = 500
encounter = 1

while True:

    if encounter == randint(1, 1): # 1,1 FOR TESTING
        story = RandomEvent(funds)
        scene_list = {1: story.old_man(), 2: story.robber(), 3: story.pretty_woman()}
        funds = scene_list[2]  #randint(1,3)] FOR TESTING


    else: 
        print "\n\n\nWelcome to Dreams Casino."
        print "Where would you like to go?"
        print "(1) Slot Machines"
        print "(2) Roulette Table"
        print "(3) Leave the Casino"
        print "\nFunds: $", funds
        game_choice = int(raw_input("> "))
4

1 回答 1

1

您在创建字典时调用函数:

scene_list = {1: story.old_man(), 2: story.robber(), 3: story.pretty_woman()}

由于您想scene_list[1]引用您的函数,请传递函数:

scene_list = {1: story.old_man, 2: story.robber, 3: story.pretty_woman}
于 2013-07-19T05:06:07.450 回答