随机生成车牌号

随机生成车牌号

import random


# random.choice()
# random.sample()
def re_alpha():
    result = ""
    for i in range(65, 91):  # 26个大写字母Asics码 65-90
        result += chr(i)
    return result


def pd_carnum():  # 随机产生一个车牌
    alpha = re_alpha()
    head = random.choice(alpha)  # 产生车牌首字母
    num_list = list(range(10))  # 产生数字列表1-9
    alpha_list = list(alpha)  # 产生字母列表A-Z
    result_list = num_list + alpha_list  # 得到字母数字列表
    content = random.sample(result_list, 5)  # 从字母数字列表随机选取5个元素
    content = "".join([str(i) for i in content])  # 将得到的结果列表转化为字符串
    return f"京{head}{content}"


def pd_car_rsnum(i):  # 产生20个车牌,并以字典存储
    return {j: pd_carnum() for j in range(1, i + 1)}


count = 0
car_num_cslist = []  # 存储用户选择的车牌
car_pd = pd_car_rsnum(20)  # 产生20个车牌并打印
# 格式化打印车牌,一行五个

for i in car_pd:
    print(f"{i},{car_pd[i]}", end=" ")
    count += 1
    if count % 5 == 0:
        print("")

count = 0
try:
    while count < 3:
        num = int(input("请输入你要选择的车牌序号:"))
        car_num_cslist.append(car_pd[num])  # 将选择的车牌存入列表
        count += 1
    print(car_num_cslist)
except (ValueError,KeyError) as e:
    print(e)

运行结果

随机生成车牌号_第1张图片

你可能感兴趣的:(随机生成车牌号)