Python中FOR循环的条件

时间:2014-10-24 09:02:01

标签: python for-loop

我正在制作一个简单的彩票模拟器。我有两个互斥的命令行标志,因此程序执行用户指定的绘制量或直到你获得累积奖金。

for i in range(args.draws):
    perform lottery draws here  

如何以不给出args.draws的方式修改for循环 直到累积奖金被击中。我现在将args.draws默认为非常高的数字 所以在几乎所有的情况下,累积奖金被击中然后打破循环,但是在那里 更好的方法来做到这一点

这是整个代码。它不是很好我只是学习Python

import random
import argparse
def oneoutof(occurance, times):
    if occurance == 0:return 0
    return float(times)/float(occurance)

if __name__ == '__main__':
    LOTTERY_NUMBERS = [i + 1 for i in range(39)]
    WIN_LIST =[0,0,0,0]
    six_plus_one = 0
    parser = argparse.ArgumentParser(description="[Lottery]\n"
    "Example: python3 lottery.py ")
    group = parser.add_mutually_exclusive_group()

    group.add_argument("-s","--seven",action="store_true",
        help="Stop when you hit 7 right")
    group.add_argument("-d","--draws",type=int,default=1000000,
        help="Stop after number of draws")
    parser.add_argument("-n","--numbers", nargs=7, type = int,choices=range(1, 40),
        help="Lottery numbers, if not given will be randomly selected")

    args = parser.parse_args()



    if args.numbers ==None:
        print('You did not give lottery numbers so following numbers are randomly selected')
        picked_numbers = set(random.sample(LOTTERY_NUMBERS, 7))

        print(*picked_numbers)
        print('Number of lottery draws is {:,}'.format(args.draws))
    else:
        picked_numbers = set(args.numbers)
        print('Number of lottery draws is {:,}'.format(args.draws))

    for i in range(args.draws):
        lotto_numbers = set(random.sample(LOTTERY_NUMBERS, 7))
        remaining_nbrs = set(LOTTERY_NUMBERS).difference(lotto_numbers)
        extra_nmbs = set(random.sample(remaining_nbrs, 3))


        correct_numbers = lotto_numbers&picked_numbers
        correct_extranmbs = extra_nmbs&picked_numbers

        if len(correct_numbers) > 3 and not (len(correct_numbers) == 6 and correct_extranmbs):
            index = len(correct_numbers) - 4
            WIN_LIST[index] = WIN_LIST[index] + 1
            if len(correct_numbers) == 7 and args.seven:
                print("You hit JACKPOT {} round".format(i))
                break                
        elif len(correct_numbers) == 6 and correct_extranmbs:
            six_plus_one=six_plus_one+1



    print('4 correct %i times, one out of %g\n'
          '5 correct %i times, one out of %g\n'
          '6 correct %i times, one out of %g\n'
          '6+1 correct %i times, one out of %f\n'
          '7 correct %i times, one out of %f\n'
          %(WIN_LIST[0],oneoutof(WIN_LIST[0], args.draws),
            WIN_LIST[1],oneoutof(WIN_LIST[1], args.draws),
            WIN_LIST[2],oneoutof(WIN_LIST[2], args.draws),
            six_plus_one,float(args.draws)/float(six_plus_one) if six_plus_one else 0,
            WIN_LIST[3],oneoutof(WIN_LIST[3], args.draws)))

3 个答案:

答案 0 :(得分:0)

您可以使用while循环。

 if(args.draws.length!=0)

      for i in range(args.draws):
      perform lottery draws here

 else

     while(!jackpotHit)
        perform lottery draws here

答案 1 :(得分:0)

我会使用itertools.count

from itertools import count

draws = range(args.draw) if args.draw is not None else count()
for i in draws:
    perform lottery draws here

答案 2 :(得分:0)

我们需要在文件顶部导入:

from itertools import count, islice

然后我们可以编辑你的一些代码......

首先,将args.draws的默认值设置为None - 我们可以将其用作"无限制"。

group.add_argument("-d","--draws", type=int, default=None, help="Stop after number of draws")

然后,我们会调整您的print以考虑None无法格式化为整数,因此我们会将其替换为可能成功的最大数量float('inf')使用inf格式打印时仍显示为{:,},因此:

print('Number of lottery draws is {:,}'.format(args.draws or float('inf')))

然后,我们不是在一个范围上循环,而是在无限序列上循环,但是将它限制为n许多元素,这些元素可以是整数,也可以是None(这意味着不要t对该序列应用限制):

for i in islice(count(1), args.draws):
    # ...