Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更改莱佛士的格式_Python_Random_Sqlalchemy - Fatal编程技术网

Python 如何更改莱佛士的格式

Python 如何更改莱佛士的格式,python,random,sqlalchemy,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('raff

嗨,我很难改变生成的莱佛士的格式,因为它看起来像这个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']))
使
添加莱佛士()
如下所示:

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\u string
和更改的
print
语句。

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

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

我在哪里用Put
ticket\u string=str(ticket).zfill(8)在
print
语句的正上方回复ticket\u string=str(ticket).zfill(8)
,然后将您的
print
语句更改为我给出的语句。我有我的synatx错误,请您对代码进行更改,好吗(最后一次调用):文件“C:\Users\osman\u 000\Documents\My Data Files\add raffle.py”,在raffle.add\u raffles()文件“C:\Users\osman\u 000\Documents\My Data Files\raffle.py”,在add\u raffles print(color+“”+“”.join(ticket\u string[:4],ticket\u string[-4:])中)类型错误:join()只接受一个参数(给定2)抱歉,您需要在
join
方法周围加上额外的括号,以便参数位于元组中。我编辑了我的答案以显示它。您好,再次感谢您能否将其嵌入到代码中,以及在生成winners时,将其插入4s中。