简体   繁体   English

python类实例化另一个类

[英]python class instantiating another class

I am having trouble figuring out how to make a menu class which will instantiate a game class in python 2.7.6 I have searched other questions related to this, but still I cannot figure out how to pass parameters from one class to another. 我在弄清楚如何制作将在python 2.7.6中实例化游戏类的菜单类时遇到了麻烦,我搜索了与此相关的其他问题,但是仍然无法弄清楚如何将参数从一个类传递到另一个类。 This is also my first time asking a question on stack overflow, so I am sorry if I incorrectly past my code in here. 这也是我第一次问堆栈溢出问题,所以如果我不正确地将代码放在这里,我感到抱歉。

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

and

class Menu:
    def __init__(self):
        #self.game = Lottery(digits, betType, betAmt)
        self.start()
    def start(self):
        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? >> ")
        self.game = Lottery(self.digits, self.betType, self.betAmt)
        self.game.play()
        raw_input("Enter to play again")

Any help would be appreciated, I am new to Python and also Stack Overflow. 任何帮助将不胜感激,我是Python的新手,也是Stack Overflow的新手。 Thanks :) 谢谢 :)

Traceback (most recent call last):
  File "ILR2.py", line 81, in <module>
    Menu1 = Menu()
  File "ILR2.py", line 71, in __init__
    self.start()
  File "ILR2.py", line 78, in start
    self.game.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

This is the error I am getting when trying to run the program. 这是我在尝试运行程序时遇到的错误。 Does this help? 这有帮助吗? Sorry I forgot to post the first time 对不起,我忘了第一次发布

The problem is not with how you're instantiating the lottery class. 问题不在于您如何实例化彩票类。 That part is fine. 那部分很好。 Rather, it looks your play method in your Lottery class is buggy. 相反,它看起来像您的Lottery类中的play方法是越野车。

We can see this from looking at your exception: 通过查看您的例外情况,我们可以看到这一点:

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

Based on context, it looks like you're trying to access the digits attribute. 根据上下文,您似乎正在尝试访问digits属性。 In order to do that, you need to use self : 为此,您需要使用self

if self.digits == '3':
    pass

I think in this situation it might not make sense to have a separate class for your menu. 我认为在这种情况下,为菜单设置单独的类可能没有意义。 After all, you are storing the same information about your game in the Menu class and in the Lottery class. 毕竟,您是在Menu类和Lottery类中存储有关游戏的相同信息。

You could simplify it quite a bit by adding the menu as a method within your Lottery class. 您可以通过在Lottery类中将菜单添加为方法来将其简化很多。 Then you would not need to pass around the variables, since they are provided by the user's input anyways: 然后,您将不需要传递变量,因为无论如何它们都是由用户输入提供的:

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

I am NOT sure, as I can't see the code for the play() function, but it looks like you should use self.digits , not digits there. 我不确定,因为我看不到play()函数的代码,但是看来您应该使用self.digits ,而不是digits Just a guess thought. 只是一个猜测的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM