V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Eyon
V2EX  ›  Python

谁帮忙写个抽奖程序吧

  •  
  •   Eyon · 2012-06-26 17:37:23 +08:00 · 4528 次点击
    这是一个创建于 4316 天前的主题,其中的信息可能已经有所发展或是发生改变。
    就是从 n 个名字中随机抽出 x 个人中奖...
    16 条回复    1970-01-01 08:00:00 +08:00
    c
        1
    c  
       2012-06-26 18:05:58 +08:00   ❤️ 1
    >>> import random
    >>> names = 'abcdefg'
    >>> print random.sample(names, 2)
    ['c', 'b']
    nigelvon
        2
    nigelvon  
       2012-06-26 18:10:14 +08:00
    python不熟,给你个思路吧,名字放到一个数组里。
    随机抽一个元素,然后从数组中删掉,直到抽到想要的数量为止。
    Eyon
        3
    Eyon  
    OP
       2012-06-26 18:11:37 +08:00
    @c names 怎么分成若干个字符串... 比如,张三 lisi 王二麻子 zhaosi
    Eyon
        4
    Eyon  
    OP
       2012-06-26 18:12:47 +08:00
    @c 最好是names能读取一个 names.txt 里面的list
    ayanamist
        5
    ayanamist  
       2012-06-26 18:16:45 +08:00   ❤️ 1
    曾经在年初的时候给年夜饭写过个类似的
    https://gist.github.com/1627198
    支持因为某个号码不在而取消某个号码重新抽奖。
    c
        6
    c  
       2012-06-26 18:20:13 +08:00
    @Eyon ayanamist 这个html版本用起来应该可以满足您的需求了
    clowwindy
        7
    clowwindy  
       2012-06-26 18:20:38 +08:00
    import random
    with open('a.txt') as f:
    print random.sample(f.read().split(' '), 2)
    Eyon
        8
    Eyon  
    OP
       2012-06-26 18:22:05 +08:00
    @c 可是不需要二等奖和三等奖...您告诉我下怎么读取names.txt 的list就是了,谢
    c
        9
    c  
       2012-06-26 18:25:34 +08:00   ❤️ 1
    https://gist.github.com/2994883

    名字放 names.txt 里, 输入 python r.py names.txt 2
    Eyon
        10
    Eyon  
    OP
       2012-06-26 18:27:32 +08:00
    @clowwindy

    with open('/Users/eyon/names.txt') as f:
    ... print random.sample(f.read().split(' '), 2)
    File "<stdin>", line 2
    print random.sample(f.read().split(' '), 2)
    ^
    IndentationError: expected an indented block
    aa88kk
        11
    aa88kk  
       2012-06-26 18:30:17 +08:00   ❤️ 1
    在线生成, 一行一个名字

    http://textmechanic.com/Random-Line-Picker.html
    Eyon
        12
    Eyon  
    OP
       2012-06-26 18:31:31 +08:00
    @c
    @aa88kk

    大榭,结贴
    clowwindy
        13
    clowwindy  
       2012-06-26 18:51:17 +08:00   ❤️ 1
    @Eyon 第三行前面加两个空格,空格被 V2EX 吃掉了。
    loading
        14
    loading  
       2012-06-26 19:36:17 +08:00
    我以前用的是网页,用js写的,能比较美观!
    winix
        15
    winix  
       2012-06-26 19:53:03 +08:00   ❤️ 1
    http://winix.name/lotto/ 去年公司年会时用JS写的,去掉了背景和名单。

    1、空格键开始,再次按空格抽一个奖
    2、z键暂停(防止误按)
    3、名单单独放在一个js文件里,第一列是手机号或者工号什么的,第二列是姓名
    4、支持作弊,目前是将特殊号码的16进制写在index.html里(一眼看不出来是谁),抽奖时按对应的数字键,从1开始
    5、窗口误关闭时会提醒
    karma
        16
    karma  
       2012-06-26 20:22:48 +08:00
    需要设计一等奖永远抽不中的功能么?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2796 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:38 · PVG 22:38 · LAX 07:38 · JFK 10:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.