2014-04-16 208 views
0

我無法弄清楚如何創建一個菜單類,它將在python 2.7.6中實例化一個遊戲類我已經搜索了與此相關的其他問題,但仍然無法弄清楚如何傳遞參數從一個班級到另一個班級。這也是我第一次問一個關於堆棧溢出的問題,所以我很抱歉,如果我在這裏錯誤地通過我的代碼。python類實例化另一個類

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 

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

任何幫助,將不勝感激,我是新來的Python,也堆棧溢出。謝謝:)

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 

這是我試圖運行該程序時得到的錯誤。這有幫助嗎?對不起,我忘了張貼在第一時間

+5

你發佈的代碼有什麼問題? – BrenBarn

+1

代碼片段不完整,併產生一個回溯,您的「彩票」類沒有'play'方法。您必須編寫該方法,或提供更完整的代碼,以便我們可以進一步提供幫助。 –

+0

同意@gddc ..顯示'play'方法的作用,請**: - )** – BorrajaX

回答

3

問題不在於如何實例化彩票類。那部分很好。相反,它看起來你的play方法在你的Lottery類是越野車。

我們可以看你的例外看到:

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 
+1

您可能意思是'self.digits',因爲'play'是類'Lottery'的函數' – JadedTuna

+0

awesome that worked !謝謝 :) – user3543047

0

我不知道,因爲我看不到的play()函數的代碼,但它看起來像你應該使用self.digits,不digits那裏。只是一個猜測的想法。

1

我認爲在這種情況下它可能沒有什麼意義,以對您的菜單單獨的類。畢竟,您在Menu課程和Lottery課程中存儲了有關遊戲的相同信息。

您可以通過在Lottery類中添加菜單作爲方法來簡化它。那麼你不需要傳遞變量,因爲它們是由用戶的輸入提供的:

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 
相關問題