2014-04-08 14 views
0

我想爲我的計算機科學課程製作一個程序,讓我們創建一個彩票遊戲生成器。這場比賽你輸入你的號碼,然後它創建門票贏得門票,以配合您的門票。所以如果你匹配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() 

我的一個同學做它有所有匹配的一對,而真實的陳述,但這會導致蟒蛇發現每個匹配集合時崩潰。關於如何讓程序停止發佈多個比賽,我沒有任何其他想法。

+0

注意記錄的變量名'了','B'等沒有多大的意義給用戶。也許你想要一個'largestMatchSoFar'變量。 – keyser

回答

0

只是保持你已經看到了

... 
costs=0 
found = [] 
while True: 
    ... 
    if matches==3 and 3 not in found: 
     found.append(3) 
     print ("You Matched 3 on try", costs) 
    elif matches==4 add 4 not in found: 
     found.append(4) 
     print ("Cool! 4 matches on try", costs) 
    ... 
    if set([3,4,5,6]).intersection(found) == set([3,4,5,6]): 
     print "You Found em all!" 
     return    
1
from random import randint, sample 

# Ontario Lotto 6/49 prize schedule 
COST = 0.50 
PRIZES = [0, 0, 0, 5., 50., 500., 1000000.] 

def draw(): 
    return set(sample(range(1, 50), 6)) 

def get_ints(prompt): 
    while True: 
     try: 
      return [int(i) for i in input(prompt).split()] 
     except ValueError: 
      pass 

def pick(): 
    while True: 
     nums = set(get_ints(
      "Please enter 6 numbers in [1..49], ie 3 4 17 22 44 47: " 
     )) 
     if len(nums) == 6 and 1 <= min(nums) and max(nums) <= 49: 
      return nums 

def num_matched(picked): 
    return len(picked & draw()) # set intersection 

def report(matches): 
    total_cost = COST * sum(matches) 
    total_won = sum(m*p for m,p in zip(matches, PRIZES)) 
    net = total_won - total_cost 
    # report on the results: 
    print("\nYou won:") 
    print(
     "  nothing {:>8} times -> ${:>12.2f}" 
     .format(sum(matches[:3]), 0.) 
    ) 
    for i in range(3, 7): 
     print(
      " ${:>12.2f} {:>8} times -> ${:>12.2f}" 
      .format(PRIZES[i], matches[i], PRIZES[i] * matches[i]) 
     ) 
    print(
     "\nYou paid ${:0.2f} to win ${:0.2f}, for a net result of ${:0.2f}." 
     .format(total_cost, total_won, net) 
    ) 

def main(): 
    # pick a set of numbers 
    picked = pick() 
    # repeat until we have seen 3, 4, 5, and 6-ball matches 
    matches = [0, 0, 0, 0, 0, 0, 0] 
    while not all(matches[3:]): 
     matches[num_matched(picked)] += 1 
    report(matches) 

if __name__=="__main__": 
    main() 

導致

Please enter 6 numbers in [1..49], ie 3 4 17 22 44 47: 4 6 9 12 14 19 

You won: 
     nothing 10060703 times -> $  0.00 
    $  5.00 181218 times -> $ 906090.00 
    $  50.00  9888 times -> $ 494400.00 
    $  500.00  189 times -> $ 94500.00 
    $ 1000000.00   1 times -> $ 1000000.00 

You paid $5125999.50 to win $2494990.00, for a net result of $-2631009.50. 
+0

非常感謝你,這個代碼不僅僅是完美的。 – user3512754

相關問題