【问题标题】:Python random lottery resultsPython 随机抽奖结果
【发布时间】:2013-07-09 23:47:32
【问题描述】:

我的任务是创建一个程序,帮助人们为彩票选择随机数,我不知道从哪里开始。程序必须:

  • 允许玩家在 1 - 5 行之间进行选择。
  • 每行 6 个数字。
  • 每个数字必须介于 1-49 之间。
  • 还有一个重复选项。

这就是我目前所拥有的:

    lines=int(input("how many lines would you like?"))
    for i in range (0,lines):
         import random
    lotterynumbers = []
     x = 0

     while x < 6:
         lotterynumbers.append(random.randint(1, 49))
         x += 1
     lotterynumbers.sort()
     print (lotterynumbers)

请帮忙。

【问题讨论】:

  • 如果这是家庭作业,您应该添加homework 标签。

标签: loops random python-3.x


【解决方案1】:

在这里,这应该会有所帮助:

from random import randint as rand_number

def create_lotter_numbers(amount=6):
    return [rand_number(1,49) for i in range(amount)]

def get_user_input(prompt="how many lines would you like? "):
    return int(input(prompt))

使用示例:

>>> a = get_user_input()
how many lines would you like? 5
>>> for i in range(a):
    create_lotter_numbers()


[47, 22, 4, 7, 41, 16]
[12, 30, 36, 1, 39, 10]
[7, 19, 7, 13, 1, 17]
[5, 26, 9, 49, 32, 22]
[32, 30, 5, 34, 45, 6]

关于限制用户输入、重复、有效性等……你可以自己解决。这个答案就在这里,这样底层代码就不会像你发布的那样有问题。

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2022-06-11
    • 2018-08-31
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多