python什么是类的实例化_python类实例化另一个类

这篇博客探讨了如何在Python中创建一个Lottery类,该类用于实例化一个游戏类。类包含了初始化方法和play方法,允许用户选择机器随机选择(MP)或手动输入(AP)彩票号码,并模拟赢得彩票所需的游戏次数。博客作者是Python新手,遇到了如何在类之间传递参数的问题。
摘要由CSDN通过智能技术生成

在python2.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

以及

^{pr2}$

任何帮助都将不胜感激,我是Python新手,也堆栈溢出。谢谢:)

^{3}$

这是我在尝试运行程序时遇到的错误。这有帮助吗?对不起,我第一次忘了投递

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值