python - Python 中全局语句的替代方法

标签 python python-2.7

我正在编写一个非常简单的轮盘模拟器,只专注于红/黑投注(基本上就像正面或反面的游戏)。

我遇到的问题是跨函数调用变量。

这是代码(我敢肯定它还有其他问题,但我现在主要关注这个问题):

import random

# Defines initial amounts of money and losses
money = 50
losses = 0

# Starts the sim
def roulette_sim():
    print "How much do you want to bet?"
    bet = int(raw_input("> "))   
    if bet > money:
        bet_too_much()
    else:
        red_or_black()


# Prevents one from betting more money than one has
def bet_too_much():
    print "You do not have all that money. Please bet again." 
    raw_input("Press ENTER to continue> ")
    roulette_sim()

# Asks user to select red or black, starts the sim, modifies money/losses
def red_or_black():
    print "OK, you bet %r" %  (bet)
    print "Red or black?"
    answer = raw_input("> ")
    number = random.randint(1, 2)
    if number == 1 and answer == "red":
        print "You win!"
        money += bet
        print "You now have %r money" % (money)
        print "Your losses are %r" % (losses)
        replay()
    elif number == 2 and answer == "black":
        print "You win!"
        money += bet
        print "You now have %r money" % (money)
        print "Your losses are %r" % (losses)
        replay()
    else:
        print "You lost!"
        money -= bet
        losses += bet
        print "You now have %r money" % (money)
        print "Your losses are %r" % (losses)
        replay()

# Asks user whether he/she wants to play again
def replay():
    print "Do you want to play again?"
    play_again = raw_input("y/n> ")
    if play_again == "y":
        roulette_sim()
    else:
        print "OK, bye loser!"


roulette_sim()

因此,我得到了 NameError: global name 'bet' is not defined,我可以通过使用 global 来避免这种情况,但我不想诉诸于此。在 roulette_sim 中,除了使用 global 之外,我如何在其他函数中使用用户分配给它的值来调用这个变量?

我想同样的问题也适用于 money 变量。

我目前对 Python 有非常基础的了解,所以对于任何错误,我深表歉意。

谢谢

最佳答案

使用类:

import random

class RouletteSim(object):
    def __init__(self):
        # Defines initial amounts of money and losses
        self.money = 50
        self.losses = 0

    # Starts the sim
    def roulette_sim(self):
        print "How much do you want to bet?"
        bet = int(raw_input("> "))
        if bet > self.money:
            self.bet_too_much()
        else:
            self.red_or_black(bet)

    # Prevents one from betting more money than one has
    def bet_too_much(self):
        print "You do not have all that money. Please bet again."
        raw_input("Press ENTER to continue> ")
        self.roulette_sim()

    # Asks user to select red or black, starts the sim, modifies money/losses
    def red_or_black(self, bet):
        print "OK, you bet %r" %  (bet)
        print "Red or black?"
        answer = raw_input("> ")
        number = random.randint(1, 2)
        if number == 1 and answer == "red":
            print "You win!"
            self.money += bet
            print "You now have %r money" % (self.money)
            print "Your losses are %r" % (self.losses)
            self.replay()
        elif number == 2 and answer == "black":
            print "You win!"
            self.money += bet
            print "You now have %r money" % (self.money)
            print "Your losses are %r" % (self.losses)
            self.replay()
        else:
            print "You lost!"
            self.money -= bet
            self.losses += bet
            print "You now have %r money" % (self.money)
            print "Your losses are %r" % (self.losses)
            self.replay()

    # Asks user whether he/she wants to play again
    def replay(self):
        print "Do you want to play again?"
        play_again = raw_input("y/n> ")
        if play_again == "y":
            self.roulette_sim()
        else:
            print "OK, bye loser!"

RouletteSim().roulette_sim()

关于python - Python 中全局语句的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18619764/

相关文章:

python - 使用 xlsxwriter 中的 Workbook 对象时,Workbook 对象没有属性 'add_sheet'

python - Airflow:如何确保 DAG 每 5 分钟运行一次?

python - 2个连续行之间的时间差

Python队列模块卡住了

python - “模块”对象没有属性 'SummaryWriter'

python - 在 docker 容器内导入 socketio 失败?

linux - Tensorflow资源耗尽但没有资源耗尽

python - PyQT4 WheelEvent?如何检测车轮是否被使用过?

python - Yocto setup.py 需要 pyserial

python - 从另一类的一个类调用函数时发生TypeError