首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python随机抽奖结果

Python随机抽奖结果
EN

Stack Overflow用户
提问于 2013-07-11 15:46:51
回答 1查看 4.7K关注 0票数 0

我被设定的任务是创建一个程序,帮助人们为彩票挑选随机数字,而我不知道start.The程序必须在哪里:

  • 允许玩家在1-5行之间选择.
  • 每一行6个号码。
  • 每个数字必须在1-49之间。
  • 还有一个重复的选择。

到目前为止,这就是我所拥有的一切:

代码语言:javascript
复制
    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)

请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-11 15:56:08

在这里,这应该是有帮助的:

代码语言:javascript
复制
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))

使用实例:

代码语言:javascript
复制
>>> 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]

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17597891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档