Python

Python的第六天

一、random 模块

 import random-------导入random模块

 1、 random.choice("abcdef")-----参数也可以是一个列表

    'c'

 2、 s = "abcdefgh"

    random.sample(s,3)-----从数据源s中随机取出3个值

    ['b','a','c']

 3、"".join(["a","b","c"])

   'abc'

 4、random.randint(1,100)-------打印一个随机数

   79

二、string 模块

  import string------导入string 模块

  1、string.ascii_letters

   'abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

  2、string.acsii_uppercase-----大写字母

   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

  3、string.acsii_lowercase-------小写字母

   'abcdefghijklmnopqrstuvwxyz'

  4、string.punctuation-----打印特殊符号

   ‘!#&%¥’

  5、string.digits------打印数字

   '123456'

二、打印车牌号码

  

import string
import random
count = 0
while count < 3:
print(" 可用号牌查询结果 ")
car_number = []
for i in range(20):
n1 = random.choice(string.ascii_uppercase )
a = string.ascii_uppercase + string.digits
n2 = "".join(random.sample(a,5))
car = f"京{n1}-{n2}"
car_number.append(car)
print(i + 1,car)
choice = input("输入你喜欢的号码号:").strip()-------a.strip() 用于删除用户不小心输入的空格,只能删除所输内容前、后的空格
if choice in car_number :
print(f"恭喜您选了新号码{choice}")
exit("good luck")
else:
print("您输入的号码不合法")
count += 1

 

原文地址:https://www.cnblogs.com/sxy2021/p/14308287.html