zoukankan      html  css  js  c++  java
  • 基础练习

    1.购物车

    2.三级菜单

    3.猜数字

    4.打印进度条

    python基础之购物车练习:

    1.输入月薪打印商品列表,

    2.选购商品,显示余额,

    3.可随时退出,并打印所购商品及余额。

     1 product_list = [
     2     ("iphone",5000),
     3     ("MacPro",10000),
     4     ("Book",120),
     5     ("bicycle",1000),
     6     ("TV",2000),
     7     ("washer",3000),
     8     ("pen",50)
     9 ]
    10 shopping_list = []
    11 salary = input("Input your salary : ")
    12 if salary.isdigit():
    13     salary = int(salary)
    14     while True:
    15         for index,item in enumerate(product_list):
    16             print(index,item)
    17         user_choice = input("what do you want to buy,or enter q to leave: ")
    18         if user_choice.isdigit():
    19             user_choice = int(user_choice)
    20             if user_choice >= 0 and user_choice < len(product_list):
    21                 p_item = product_list[user_choice]
    22                 if salary >= p_item[1]:
    23                     shopping_list.append(p_item[0])
    24                     salary = salary-p_item[1]
    25                     print("add your choice into shoppingcart,your current balance is 33[31;1m%s33[0m." %salary)
    26                 else:
    27                     print("you have money only 33[31;1m%s33[0m,can't payment"%salary)
    28             else:
    29                 print("your choice is outof selection")
    30         elif user_choice == "q":
    31             print("-----shopping cart-----")
    32             for p in shopping_list:
    33                 print(p)
    34             exit()
    35         else:
    36             print("invaild option")
    37 else:
    38     print("Please input the digit")
    View Code

    python基础之简单版三级菜单:

     1 data = {
     2     "北京":{},
     3     "福建":{
     4         "厦门":{
     5             "鼓浪屿":["日光岩","淑庄花园"],
     6             "思明区":["南普陀","厦门大学"],
     7             "湖里区":["湖里公园","保税区"]
     8         },
     9         "漳州":{},
    10         "泉州":{}
    11     },
    12     "广东":{}
    13 }
    14 
    15 exit_flag = False
    16 while not exit_flag:
    17     for i in data:
    18         print(i)
    19     choice = input("what's your option1 :")
    20     if choice in data:
    21         while not exit_flag:
    22             for i2 in data[choice]:
    23                 print(i2)
    24             choice2 = input("what's your option2 :")
    25             if choice2 in data[choice]:
    26                 while not exit_flag:
    27                     for i3 in data[choice][choice2]:
    28                         print(i3)
    29                     choice3 = input("what's your option3 :")
    30                     if choice3 in data[choice][choice2]:
    31                         for i4 in  data[choice][choice2][choice3]:
    32                             print(i4)
    33                         last_1 = input("the last menu,enter b back:")
    34                         if last_1 == "b":
    35                             pass
    36                         elif last_1 == "q":
    37                             exit_flag = True
    38                     elif choice3 == "b":
    39                         break
    40                     elif choice3 == "q":
    41                         exit_flag =True
    42             elif choice2 == "b":
    43                 break
    44             elif choice2 == "q":
    45                 exit_flag =True
    46     elif choice == "q":
    47         exit_flag = True
    48     elif choice not in data:
    49         print("Please input the 33[1;31;40mRight33[0m option")
    View Code
    menu = {
        '北京': {
            '海淀': {
                '五道口': {
                    'soho': {},
                    '网易': {},
                    'google': {}
                },
                '中关村': {
                    '爱奇艺': {},
                    '汽车之家': {},
                    'youku': {},
                },
                '上地': {
                    '百度': {},
                },
            },
            '昌平': {
                '沙河': {
                    '老男孩': {},
                    '北航': {},
                },
                '天通苑': {},
                '回龙观': {},
            },
            '朝阳': {},
            '东城': {},
        },
        '上海': {
            '闵行': {
                "人民广场": {
                    '炸鸡店': {}
                }
            },
            '闸北': {
                '火车战': {
                    '携程': {}
                }
            },
            '浦东': {},
        },
        '山东': {},
    }
    
    #堆栈的思想
    # l = [menu]
    # while l:
    #     for key in l[-1]:
    #         print(key)
    #     k = input('input>>').strip()   # 北京
    #     if k in l[-1].keys() and l[-1][k]:l.append(l[-1][k])
    #     elif k == 'b':
    #         l.pop()
    #     elif k == 'q':
    #         break
    
    #递归的思想
    def three_level_menu(menu):
        while True:
            for key in menu:
                print(key)
            k=input('>>>')
            if k=='q':return 'q'
            elif k=='b':break
            elif k in menu:
                ret=three_level_menu(menu[k])
                if ret=='q':return 'q'
    three_level_menu(menu)
    三级菜单堆栈和递归

    python基础之猜数字:

    打印进度条

    #first
    import  time,sys
    for i in range(10):
        sys.stdout.write("#")
        sys.stdout.flush()
        time.sleep(0.5)
    
    
    
    #second
    import time
    for i in range(0,101,2):
        time.sleep(0.1)
        char_num = i//2   #打印多少个*    #
    相当于seek(0) 即回到行首
        per_str = '
    %s%% : %s
    ' % (i,'*' * char_num) if i==100 else '
    %s%% : %s' %(i,'*' *char_num)
        print(per_str,end='',flush=True)
    #progress Bar 专门做进度条的模块,可以研究一下
  • 相关阅读:
    tp6 控制器不存在:appindexcontrollerIndex
    thinkphp助手函数
    thinkphp5.1、thinkphp6
    lnmp安装
    关于像秒杀这种高并发场景的解决方案
    C# 基于hslcommunication的异步,同步,一对多,webapi等跨程序网络框架实现,适用程序-程序通信
    C# 调用Python脚本 python脚本反调用C#方法 python脚本调用另一个脚本
    C# webapi服务器,创建自定义的,简单的web服务器,供远程接口调用
    C# 创建mqtt服务器,mqtt发布数据,验证逻辑
    C# 实现 websocket 服务器 发送客户端网页前端数据,C#和网页前端通信
  • 原文地址:https://www.cnblogs.com/gkx0731/p/9419545.html
Copyright © 2011-2022 走看看