python类实例化另一个类

python类实例化另一个类,python,class,python-2.7,Python,Class,Python 2.7,在python 2.7.6中,我很难弄清楚如何创建一个菜单类来实例化一个游戏类。我已经搜索了与此相关的其他问题,但仍然无法弄清楚如何将参数从一个类传递到另一个类。这也是我第一次问关于堆栈溢出的问题,所以如果我在这里错误地跳过了我的代码,我很抱歉 class Lottery: def __init__(self, digits, betType, betAmt): self.digits = digits self.betType = betType

在python 2.7.6中,我很难弄清楚如何创建一个菜单类来实例化一个游戏类。我已经搜索了与此相关的其他问题,但仍然无法弄清楚如何将参数从一个类传递到另一个类。这也是我第一次问关于堆栈溢出的问题,所以如果我在这里错误地跳过了我的代码,我很抱歉

class Lottery:
    def __init__(self, digits, betType, betAmt):
        self.digits = digits
        self.betType = betType
        self.betAmt = betAmt

    def play(self):
        print "<>" * 40
        print "Use 'mp' to play the same 'machine pick' each time"
        print "Use 'ap' to play a new 'machine pick' number each time"
        print "<>" * 40
        guess = raw_input("Choose a lottery number and see how long it takes until you win! >>")
        if guess.upper() == 'MP': #added machine pick option
            if digits == '3': #attempt support for 4 and 5 digit numbers
                choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            elif digits == '4':
                choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            elif digits == '5':
                choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            else:
                pass
        elif guess.upper() == 'AP': #placeholder for autopick in main loop
            pass
        else:
            choice = guess
        tries = 0
        while True:
            if guess.upper() == 'AP': #added machine pick option
                if digits == '3': #attempt support for 4 and 5 digit numbers
                    choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
                elif digits == '4':
                    choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
                elif digits == '5':
                    choice = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            if digits == '3': #attempt support for 4 and 5 digit numbers
                winning = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            elif digits == '4':
                winning = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            elif digits == '5':
                winning = str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9)) + str(randint(0,9))
            print winning, choice
            tries += 1
            if digits == '3':
                time.sleep(0.02)
            elif digits == '4':
                time.sleep(0.002)
            else:
                time.sleep(0.0005)
            if winning == choice:
                print "-----" * 10
                print "winner after " + str(tries) + " tries!"
                print "It took you " + str(tries/2) + " days to win!"
                print "your tickets cost $" + str(tries) + ".00"
                print "Your payout was $500"
                print "Your Net Revenue was $" + str(500 - tries) + ".00"
                print "-----" * 10
                break
任何帮助都将不胜感激,我对Python和堆栈溢出都是新手。谢谢:)

回溯(最近一次呼叫最后一次):
文件“ILR2.py”,第81行,在
Menu1=菜单()
文件“ILR2.py”,第71行,在_init中__
self.start()
文件“ILR2.py”,第78行,开头
self.game.play()
文件“ILR2.py”,第38行,正在播放中
如果数字=='3':#尝试支持4位和5位数字
NameError:未定义全局名称“digits”

这是我在尝试运行程序时遇到的错误。这有用吗?很抱歉,我第一次忘了发布我不确定,因为我看不到
play()
函数的代码,但看起来您应该使用
self.digits
,而不是
digits
。只是一个猜测。

我不确定,因为我看不到
play()
函数的代码,但看起来应该使用
self.digits
,而不是
digits
。只是一个猜测。

问题不在于如何实例化彩票类。那部分很好。相反,在
lotking
类中,您的
play
方法似乎有缺陷

我们可以通过查看您的异常来了解这一点:

File "ILR2.py", line 38, in play
    if digits == '3': #attempt support for 4 and 5 digit numbers
NameError: global name 'digits' is not defined
根据上下文,您似乎正在尝试访问
digits
属性。为此,您需要使用
self

if self.digits == '3':
    pass

问题不在于如何实例化彩票类。那部分很好。相反,在
lotking
类中,您的
play
方法似乎有缺陷

我们可以通过查看您的异常来了解这一点:

File "ILR2.py", line 38, in play
    if digits == '3': #attempt support for 4 and 5 digit numbers
NameError: global name 'digits' is not defined
根据上下文,您似乎正在尝试访问
digits
属性。为此,您需要使用
self

if self.digits == '3':
    pass

我认为在这种情况下,为菜单单独设置一个类可能没有意义。毕竟,您在
菜单
类和
彩票
类中存储了相同的游戏信息

您可以通过在
lotking
类中添加菜单作为方法来简化它。那么您就不需要传递变量,因为它们是由用户的输入提供的:

class Lottery:    
    def __init__(self):
        self.showMenu()
        self.play()

    def showMenu(self):
        # this is the code from your Menu class
        print "Welcome to the Lottery!"
        self.digits = raw_input("Would you like to play '3' digit, '4' digit, or '5' digit? >> ")
        self.betType = raw_input("Straight, or Boxed bet type? >> ")
        self.betAmt = raw_input("$0.50, or $1.00? >> ")

    def play(self):
        # gameplay code as before

我认为在这种情况下,为菜单单独设置一个类可能没有意义。毕竟,您在
菜单
类和
彩票
类中存储了相同的游戏信息

您可以通过在
lotking
类中添加菜单作为方法来简化它。那么您就不需要传递变量,因为它们是由用户的输入提供的:

class Lottery:    
    def __init__(self):
        self.showMenu()
        self.play()

    def showMenu(self):
        # this is the code from your Menu class
        print "Welcome to the Lottery!"
        self.digits = raw_input("Would you like to play '3' digit, '4' digit, or '5' digit? >> ")
        self.betType = raw_input("Straight, or Boxed bet type? >> ")
        self.betAmt = raw_input("$0.50, or $1.00? >> ")

    def play(self):
        # gameplay code as before

您发布的代码有什么问题?代码片段不完整,并生成一个回溯,即您的
彩票
类没有
play
方法。您必须编写该方法,或提供更完整的代码,以便我们进一步提供帮助。同意@g.d.d.c。。。显示
play
方法的作用,请:-)您发布的代码有什么问题?代码片段不完整,并产生一个回溯,即您的
彩票
类没有
play
方法。您必须编写该方法,或提供更完整的代码,以便我们进一步提供帮助。同意@g.d.d.c。。。请显示
play
方法的功能:-)您可能是指
self.digits
,因为
play
是类
lotking
的函数,您可能是指
self.digits
,因为
play
是类
lotking