基礎練習

1.購物車python

2.三級菜單git

3.猜數字app

4.打印進度條iphone

 

python基礎之購物車練習:ide

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

2.選購商品,顯示餘額,spa

3.可隨時退出,並打印所購商品及餘額。code

 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基礎之簡單版三級菜單:blog

 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 專門作進度條的模塊,能夠研究一下
相關文章
相關標籤/搜索