[英]How do I promt user x amount of times based on their input on the first question
[英]How to loop x amount of times based on user input
我正在尝试解决这个示例问题,该问题询问用户他们想生成多少张强力球票,然后仅针对他们这样做。
到目前为止,我有以下内容:
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的新手,除了自己能学到的东西外,没有太多其他说明,因此,我完全陷入了停顿。 我会感谢任何人的帮助。 谢谢!
将for i in num_tickets():
更改for i in num_tickets():
将for i in xrange(num_tickets):
更改for i in num_tickets():
for i in xrange(num_tickets):
num_tickets
是整数。 xrange(num_tickets)
生成序列0,1,.. num_tickets - 1
要迭代一个值,请使用range和xrange函数。
for i in range(num_tickets)
n=int(input())
for i in range(0,n):
n=int(input())
您可以在此循环中输入n个值,并且它将运行n次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.