如何让我的程序在找到匹配后停止使用一行?

时间:2014-04-08 21:03:02

标签: python

我正在尝试为我的计算机科学课程制作一个程序,让我们创建一个彩票游戏生成器。这个游戏让你输入你的号码,然后它创建门票赢得门票,以匹配你的票。因此,如果你匹配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()

我的一个同学让每个匹配对都有一个真正的声明,但这会导致python在找到每个匹配集时崩溃。关于如何使程序停止发布多个匹配,我没有其他想法。

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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