我怎样才能改变抽奖的形式

2024-05-23 22:12:38 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我有麻烦改变生成的莱佛士的格式,因为它看起来像这个水49250097站,我试图实现的是像这个水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']))

Tags: inemailcreatetablecolumnrandomticketdatabase
2条回答

使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语句

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

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

您正在独立生成票证;两次生成相同的值是不太可能的,但也是可能的,而且赔率随票数的增加而线性增加。如果生成一万张票,至少有一张重复的概率约为4%;2万张,约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

相关问题 更多 >