无法根据python中的用户输入重复代码

时间:2014-08-30 15:49:13

标签: python

我在一周前开始编程,我决定编写一个随机分配欧洲彩票号码的程序。我有一个工作脚本,给出5个数字和2个明星进行下注。但是,我希望程序能够打印出用户想要的多次下注而无需多次运行程序。我试图创建一个类,它将被实例化为用户需要的次数,但它不起作用。这是我现在的脚本:

import random

print "This program doesn't increase the chances of winning the lottery!"
numbers[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]

stars=[1,2,3,4,5,6,7,8,9,10,11]

a=random.sample(numbers, 5)

a.sort()

b=random.sample(stars, 2)

b.sort()

print "The numbers are", a

print "The stars are", b

我理解为了从用户那里得到输入我需要使用raw_input(“输入你想要的投注数量”)但是我不知道如何使用该输入来重复代码。此外,如果有人知道如何缩短列表,就像不必写入1到50之间的每个数字,只需写入1-50,我将不胜感激。对不起英语不好,提前谢谢。

2 个答案:

答案 0 :(得分:0)

将代码置于循环中。

import random
numbers = range(1,51)  # [1,2,3,... 49,50]
stars = range(1,12)    # [1,2,3,... 10,11]

def makeNumbers(plays):
    for i in range(plays):  # makes numbers and stars i times
        a = sorted(random.sample(numbers, 5))
        b = sorted(random.sample(stars,2))
        print 'numbers: ', a
        print 'stars: ', b

测试功能

>>> makeNumbers(3)
numbers:  [3, 25, 40, 41, 47]
stars:  [8, 11]
numbers:  [22, 25, 42, 47, 50]
stars:  [5, 9]
numbers:  [6, 23, 34, 40, 44]
stars:  [5, 7]

答案 1 :(得分:0)

给出一个数字:

number = int(raw_input("Enter number of bets you want here"))

for i in range(0,number+1):
    # here you will write your code and it will be repeated number times

如果要创建1到50的列表,请使用范围:

range(1,51)