我无法弄清楚如何创建一个菜单类,它将在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
和
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和Stack Overflow的新手。谢谢:))
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
这是我在尝试运行程序时遇到的错误。这有帮助吗?对不起,我忘了第一次发帖
答案 0 :(得分: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 :(得分: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
答案 2 :(得分:0)
我不确定,因为我看不到play()
函数的代码,但看起来你应该使用self.digits
,而不是digits
。只是一个猜测。