如何更改抽奖的格式

时间:2016-02-07 15:01:21

标签: python random sqlalchemy

嗨我无法查看生成的抽奖活动的格式,因为它看起来像这样的Aqua 49250097和我试图实现的是这样的Aqua 4925 0097并且我没有得到任何抽奖与0如蓝色0223 4773。 继承我的代码

from sqlalchemy import *
import random

engine = create_engine('sqlite:///raffle.db')

metadata = MetaData(bind=engine)

raffles_table = Table('raffles', metadata,
        Column('id', Integer, primary_key=True),
        Column('email', String(40)),
        Column('raffle_color', String(40)),
        Column('raffle_ticket', Integer),
        )

# create tables in database
metadata.create_all(checkfirst=True)

# create a database connection
conn = engine.connect()

def add_raffles():
    email = input("Please enter your email address:")
    num_tickets = int(input("How many tickets do you want:"))

    for i in range(num_tickets):
        ins_raffle = raffles_table.insert()
        colors = ['blue','Pink','Plum','Aqua','Navy','Grey','Rose','Ruby','Teal','Gold','Jade','Lime']
        color = random.choice(colors)
        ticket = random.randrange(10 ** 8)
        new_raffle = ins_raffle.values(email = email, raffle_color = color, raffle_ticket = ticket)
        # add raffle to database by executing SQL
        conn.execute(new_raffle)
        print(color + "   " + str(ticket))

def select_winner():
    winner_query = raffles_table.select().order_by(func.random()).limit(2)
    winner = conn.execute(winner_query)
    for row in winner:
        print("The winner is:" + row['email'])
        print("The winning raffle is:" + row['raffle_color'] +"  " + str(row['raffle_ticket']))

2 个答案:

答案 0 :(得分:1)

add_raffles()看起来像这样:

def add_raffles():
    email = input("Please enter your email address:")
    num_tickets = int(input("How many tickets do you want:"))

    for i in range(num_tickets):
        ins_raffle = raffles_table.insert()
        colors = ['blue','Pink','Plum','Aqua','Navy','Grey','Rose','Ruby','Teal','Gold','Jade','Lime']
        color = random.choice(colors)
        ticket = random.randrange(10 ** 8)
        new_raffle = ins_raffle.values(email = email, raffle_color = color, raffle_ticket = ticket) 
        # add raffle to database by executing SQL 
        conn.execute(new_raffle)
        ticket_string = str(ticket).zfill(8)
        print(color + "    " + " ".join((ticket_string[:4], ticket_string[-4:])))

注意,最后添加了ticket_string和更改的print声明。

答案 1 :(得分:1)

您无缘无故地重复定义colors。将其移动到脚本的顶部并使其成为常量,即

COLORS = "Blue Pink Plum Aqua Navy Grey Rose Ruby Teal Gold Jade Lime".split()

您正在独立生成门票;它不太可能,但可能两次生成相同的值,并且赔率与票数相比增加超过线性。如果你产生一万张票,那么至少有一张票的概率约为4%;两万张票,约占15%;十万张门票超过98%。根据您的使用情况,您可能不在乎,但需要牢记的是(您对两个大奖的兴趣如何?)。

根据一个人通常购买的门票数量,您可以通过将电子邮件放在单独的表格中来节省一些空间。您还可以通过存储单个整数

来节省相当多的空间
BASE = 10 ** 8
NUM_COLORS = len(COLORS)

ticket = random.randrange(NUM_COLORS * BASE)

并且仅将其拆分显示,例如

color_index, rem = divmod(ticket, BASE)
color = COLORS[color_index]
num_a, num_b = divmod(rem, 10 ** 4)
print("Your ticket is: {} {:04d} {:04d}".format(color, num_a, num_b))

给出了像

这样的结果
Your ticket is: Lime 2592 1700
Your ticket is: Navy 0828 6111
Your ticket is: Lime 3741 7599
Your ticket is: Ruby 4017 4645
Your ticket is: Aqua 0556 1852
Your ticket is: Grey 2486 5298
Your ticket is: Gold 0195 8990
Your ticket is: Navy 9287 8727
Your ticket is: Blue 3736 3443
Your ticket is: Lime 9365 1980
Your ticket is: Plum 2247 9671
Your ticket is: Lime 6568 5285
Your ticket is: Pink 7591 3894
Your ticket is: Grey 6839 4780
Your ticket is: Pink 9348 9882
Your ticket is: Plum 3868 6449
Your ticket is: Rose 2588 7999
Your ticket is: Grey 0625 5061
Your ticket is: Rose 2132 8136
Your ticket is: Navy 0526 4325