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 \033[31;1m%s\033[0m." %salary)
26                 else:
27                     print("you have money only \033[31;1m%s\033[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 \033[1;31;40mRight\033[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   #打印多少个*    #\r相当于seek(0) 即回到行首
    per_str = '\r%s%% : %s\n' % (i,'*' * char_num) if i==100 else '\r%s%% : %s' %(i,'*' *char_num)
    print(per_str,end='',flush=True)
#progress Bar 专门做进度条的模块,可以研究一下

 

posted on 2018-08-04 18:48  阿橙  阅读(259)  评论(0编辑  收藏  举报