侧边栏壁纸
  • 累计撰写 208 篇文章
  • 累计创建 16 个标签
  • 累计收到 5 条评论

目 录CONTENT

文章目录

python练习脚本

Wake
2022-09-09 / 0 评论 / 0 点赞 / 638 阅读 / 1,276 字

一、购物车1.0
需求:
(1)打印产品列表里的产品
(2)写一个循环不断问用户想买什么,用户选择一个商品编号,把对应的商品编号添加到购物车,打印购物车里的商品列表


from curses.ascii import isdigit


production_list=[
    ("Iphone",6700),
    ("Macbook_pro",13000),
    ("Bike",800),
    ("sceient_book",60),
    ("Coffee",20),
    ("Airpods",2400),
    ("imac_pro",24000),
]
# 高亮输出字符串
def output_highlight(s):
      print('\033[1;31;40m%s\033[0m' % s)

salary = input("please input your salary:")
shopping_list = []
if salary.isdigit():
    salary = int(salary)
    while True:
      for index,item in enumerate(production_list):
         print(index,item) 
      user_choice = input("Please enter the number you want to purchase:")
      if user_choice.isdigit():
        user_choice = int(user_choice)
        if user_choice < len(production_list) and user_choice >= 0:
            p_item = production_list[user_choice]
            if p_item[1] < salary:
              shopping_list.append(p_item)
              salary -= p_item[1]
              output_highlight ("%s has add your shopping car,The current balance is [%s]" %(p_item,salary))     #增加高亮显示
            else:
              print("The balance is insufficient. Your current balanceis [%s]" %salary) 
              print("---------A list of items already purchased----------------")
              for p in shopping_list:
                  print(p)
              output_highlight("If the balance is insufficient, the program exits")
              exit()
        else:
            output_highlight("The shop code [%s] doesn't exist!" % user_choice)            
      elif user_choice == 'q':
        print("---------A list of items already purchased----------------")
        for p in shopping_list:
            output_highlight(p)
        output_highlight("The program is about to exit") 
        exit()
      else:
        output_highlight("input parameters invaild,the program will exit")
        exit()         
else:
    output_highlight("invaild parameters,The program is about to exit!")


    

二.简单三级菜单

menu={'中国':{'广东':{'广州':{'天河区','海珠区','南沙区'},'深圳':{'南山区','龙岗区','福田区'},'惠州':{'惠阳区','淡水区','惠城区'}},
              '北京':{'昌平':{'好','很好','非常好'},'海淀':{'好','很好','非常好'},'朝阳':{'好','很好','非常好'}},
              '山西':{'太原':{'好','很好','非常好'},'大同':{'好','很好','非常好'},'运程':{'好','很好','非常好'}}},
      '美国':{'纽约州':{'纽约市':{'好','很好','非常好'},'罗彻斯特':{'好','很好','非常好'},'水牛城':{'好','很好','非常好'}},
              '加利福利亚州':{'洛杉矶':{'好','很好','非常好'},'好莱坞':{'好','很好','非常好'},'阿罕布拉':{'好','很好','非常好'}},
              '宾夕法尼亚州':{'费城':{'好','很好','非常好'},'伯利恒':{'好','很好','非常好'},'华盛顿':{'好','很好','非常好'}}},
      '澳大利亚':{'维多利亚州':{'墨尔本':{'好','很好','非常好'},'吉朗':{'好','很好','非常好'},'巴里迪':{'好','很好','非常好'}},
              '西澳大利亚州':{'马扎尔':{'好','很好','非常好'},'柏斯':{'好','很好','非常好'},'费里曼图':{'好','很好','非常好'}},
              '南澳大利亚州':{'阿德莱德':{'好','很好','非常好'},'内陆':{'好','很好','非常好'},'巴罗莎':{'好','很好','非常好'}}}}
is_status = False
while not is_status:
    for options in menu:
        print (options)
    user_choice = input("Please enter your choice>>>")
    if user_choice in menu:
       while not is_status:
          for options2 in menu[user_choice]:
              print("\t",options2)
          user_choice2 = input("Please enter your choice2>>>")
          if user_choice2 in menu[user_choice]:
            while not is_status:
                for options3 in menu[user_choice][user_choice2]:
                    print("\t\t",options3)
                user_choice3 = input("Please enter your choice3>>>")
                if user_choice3 in menu[user_choice][user_choice2]:
                  while not is_status:
                     for options4 in menu[user_choice][user_choice2][user_choice3]:
                         print("\t\t",options4)
                     user_choice4 = input("return>>>:")
                     if user_choice4 == "b":
                        break
                     elif user_choice4 == "q":
                        is_status = True
                if user_choice3 == "b":
                        break
                elif user_choice3 == "q":
                        is_status = True
          if user_choice2 == "b":
                        break
          elif user_choice2 == "q":
                        is_status = True

三.python 生成器练习脚本

import time
def comsumer(name):
    print ("{}准备消费饺子了".format(name))
    while True:
      jiaozi = yield
      print ("饺子{0},被消费者{1}吃掉了".format(jiaozi,name))
    #return name



def produce(name):
    C = comsumer('ben')
    C1 = comsumer('fio')
    C.__next__()
    C1.__next__()
    for i in range(10):
        time.sleep(1)
        print("饺子被做好了,现在分成两份")
        C.send(i)
        C1.send(i)

produce("jack")
    

四.python 自定义本地时间函数

import time 
def log_timer():
    loger_time = time.localtime()
    print(loger_time)
    local_time=time.strftime("%Y-%m-%d %H:%M:%S",loger_time)
    print(local_time)
log_timer()

五.python随机生成6位验证码


def check_code(n=6):
    code = ''
    for i in range(n):
        r = random.randint(0,5)
        if r == 1 or r == 4:
            num = random.randint(0,9)
            code = code + str(num)
        else:
            alpha = chr(random.randrange(65,90))
            code = code + alpha
    print(code)
check_code()
0

评论区