我無法弄清楚如何創建一個菜單類,它將在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
這是我試圖運行該程序時得到的錯誤。這有幫助嗎?對不起,我忘了張貼在第一時間
你發佈的代碼有什麼問題? – BrenBarn
代碼片段不完整,併產生一個回溯,您的「彩票」類沒有'play'方法。您必須編寫該方法,或提供更完整的代碼,以便我們可以進一步提供幫助。 –
同意@gddc ..顯示'play'方法的作用,請**: - )** – BorrajaX