我想爲我的計算機科學課程製作一個程序,讓我們創建一個彩票遊戲生成器。這場比賽你輸入你的號碼,然後它創建門票贏得門票,以配合您的門票。所以如果你匹配3,它說你匹配3,4說4,5說5,在6場比賽它會停止該程序。我的問題是,如果你在第一個隨機生成的集合(非常不可能,但可能)上得到了6的匹配,它不會一直持續到3,4和5的匹配。我需要它匹配一組3 ,所以說,則忽略匹配的另一套三和只關心比賽4,5和6如何讓我的程序在找到匹配後停止使用一行?
from random import *
import random
def draw():
#return a list of six randomly picked numbers
numbers=list(range(1,50))
drawn=[]
for n in range (6):
x=randint(0,len(numbers)-1)
no=numbers.pop(x)
drawn.append(no)
return drawn
a=int(input("What is your first number? (maximum of 49)"))
b=int(input("What is your second number? (different from 1)"))
c=int(input("What is your third number? (different from 1,2)"))
i=int(input("What is your fourth number?(different from 1,2,3)"))
e=int(input("What is your fith number?(different from 1,2,3,4)"))
f=int(input("What is your sixth number?(different from 1,2,3,4,5)"))
def winner():
ticket=[a,b,c,i,e,f]
wins=0
costs=0
while True:
costs=costs+1
d=draw()
matches=0
for h in ticket:
if h in d:
matches=matches+1
if matches==3:
print ("You Matched 3 on try", costs)
elif matches==4:
print ("Cool! 4 matches on try", costs)
elif matches==5:
print ("Amazing!", costs, "trys for 5 matches!")
elif matches==6:
print ("Congratulations! you matched all 6 numbers on try", costs)
return False
draw()
winner()
我的一個同學做它有所有匹配的一對,而真實的陳述,但這會導致蟒蛇發現每個匹配集合時崩潰。關於如何讓程序停止發佈多個比賽,我沒有任何其他想法。
注意記錄的變量名'了','B'等沒有多大的意義給用戶。也許你想要一個'largestMatchSoFar'變量。 – keyser