如何根据用户输入循环x次 [英] How to loop x amount of times based on user input

查看:58
本文介绍了如何根据用户输入循环x次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决这个示例性问题,该问题询问用户他们想生成多少张强力球票,然后仅对他们这样做.

I'm trying to work on this sample problem that asks a user how many power ball tickets they would like generated, and then it does just that for them..

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

So far, I have the following:

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)

当我尝试运行此命令时,出现以下错误:

When I try to run this, I'm getting the following error:

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的新手,除了自己能学到的东西外,没有太多其他说明,因此,我完全陷入了停顿.我会感谢任何人的帮助.谢谢!

I'm somewhat new to python, and haven't had much instruction other than what I've managed to learn on my own, and for that reason, I've completely come to a standstill. I would appreciate anyone's help. Thank you!

推荐答案

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

For iterating up to a value, use the range and xrange functions.

for i in range(num_tickets)

这篇关于如何根据用户输入循环x次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆