这是个模拟购买彩票的题,有一组固定的中奖号码,然后每次会随机给出一组号码,来进行匹配,看是否中奖,这里我已经写好了。
我的问题是:如果我还想算出要买多少张彩票才能至少中3个白球,这该怎么写。求帮我解答一下
```python
import random
print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
mywhite = random.sample(range(1, 69), 5)
myred = random.randint(1, 26)
white = [32, 35, 40, 52, 54]
red = 1
print("Your selection for white balls are: ", *mywhite )
print("Your selection for red balls is: ", myred )
print("Winning numbers for white balls:", *white)
print("Winning number for red ball:", red)
matchwhite = 0
for i, j in zip(mywhite, white):
if i == j:
matchwhite += 1
matchred = 1 if myred == red else 0
if matchwhite == 0 and matchred ==0:
print ("No matching. Thanks for your contribution!")
elif matchred:
if matchwhite == 0:
print ("Matching only the red ball: $4")
elif matchwhite == 1:
print ("Matching the red ball and one white ball: $4")
elif matchwhite == 2:
print ("Matching the red ball and two white balls: $7")
elif matchwhite == 3:
print ("Matching the red ball and three white balls: $100")
elif matchwhite == 4:
print ("Matching the red ball and four white balls: $10,000")
else:
print ("Matching five white balls and the red ball: Jackpot")
else:
if matchwhite == 3:
print ("Matching three white balls: $7")
elif matchwhite == 4:
print ("Matching four white balls: $100")
else:
print ("Matching five white balls: $1,000,000")
```
写个循环计数就行
import random
print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
mywhite = random.sample(range(1, 69), 5)
myred = random.randint(1, 26)
white = [32, 35, 40, 52, 54]
red = 1
print("Your selection for white balls are: ", *mywhite)
print("Your selection for red balls is: ", myred)
print("Winning numbers for white balls:", *white)
print("Winning number for red ball:", red)
matchwhite = 0
for i, j in zip(mywhite, white):
if i == j:
matchwhite += 1
matchred = 1 if myred == red else 0
if matchwhite == 0 and matchred == 0:
print("No matching. Thanks for your contribution!")
elif matchred:
if matchwhite == 0:
print("Matching only the red ball: $4")
elif matchwhite == 1:
print("Matching the red ball and one white ball: $4")
elif matchwhite == 2:
print("Matching the red ball and two white balls: $7")
elif matchwhite == 3:
print("Matching the red ball and three white balls: $100")
elif matchwhite == 4:
print("Matching the red ball and four white balls: $10,000")
else:
print("Matching five white balls and the red ball: Jackpot")
else:
if matchwhite == 3:
print("Matching three white balls: $7")
elif matchwhite == 4:
print("Matching four white balls: $100")
else:
print("Matching five white balls: $1,000,000")
cnt=0
x=0
while cnt<3:
mywhite = random.randint(1,69)
if mywhite in white:
cnt+=1
x+=1
print('至少'+str(x)+'次')