如何根据用户输入循环x次

时间:2016-05-16 17:46:55

标签: python loops

我正在尝试处理这个问题,询问用户他们想要生成多少个强力球票,然后它就是为他们做的..

到目前为止,我有以下内容:

import random

first_five_nums = range(1, 54)  # first five number possibilities
powerball_nums = range(1, 43)  # powerball number possibilities
num_to_select = 5  # to select 5 random numbers for the ticket

five_generated_nums = random.sample(first_five_nums, num_to_select)  # first five numbers
powerball_generated = random.choice(powerball_nums)  # powerball number

print ("Please enter the amount of Powerball tickets you would like to be generated..")
num_tickets = int(raw_input("Number of tickets: "))

for i in num_tickets():
    print ("Your numbers: %s, Powerball: %s") % (five_generated_nums, powerball_generated)

当我尝试运行时,我收到以下错误:

Traceback (most recent call last):
  File "D:\aa_grimes\my_python_works\powerball_generator.py", line 13, in <module>
    for i in num_tickets():
TypeError: 'int' object is not callable

我对python有点新鲜,除了我自己学到的东西之外没有太多的指导,因此,我完全陷入了停滞状态。我很感激任何人的帮助。谢谢!

3 个答案:

答案 0 :(得分:3)

for i in num_tickets():更改为for i in xrange(num_tickets):

num_tickets是整数。 xrange(num_tickets)生成序列0,1,... num_tickets - 1

答案 1 :(得分:1)

要迭代一个值,请使用range和xrange函数。

for i in range(num_tickets)

答案 2 :(得分:0)

n=int(input())
for i in range(0,n):
    n=int(input())

您可以在此循环中输入n个值,并且它将运行n次。