嗨,我有麻烦改变产生的抽奖的格式,因为它看起来像这个水49250097,我试图实现的是像这个水49250097,我没有得到任何抽奖开始与0,例如像蓝色02234773。下面是我的代码
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']))发布于 2016-02-07 23:08:38
让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语句。
发布于 2016-02-08 02:42:37
您无缘无故地重复定义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 4325https://stackoverflow.com/questions/35255059
复制相似问题