1 # Author: Jason Zhu
2 3 ''' 4 需求: 5 1. 啟動程式后,讓用戶輸入工資,然后列印商品串列 6 2. 允許用戶根據商品編號購買商品 7 3. 用戶選擇商品后,檢測余額是否充足,是就直接購買,否則就提示-您余額不足, 8 4. 可隨時退出程式,退出時,列印已經購買的商品和您當前賬戶的余額 9 ''' 10 11 product_list = [ 12 ('小米手機',3999), 13 ('三星冰箱',12999), 14 ('羅技鍵盤',998), 15 ('Python工具書',52) 16 ] 17 18 shopping_list =[] 19 salary = input('請輸入您的工資>>') 20 if salary.isdigit(): # 判斷輸入是否為 數字 21 salary = int(salary) 22 while True: 23 for num,item in enumerate(product_list): 24 print(num,item) 25 26 user_choice = input('選擇您要購買的商品編號>>') 27 if user_choice.isdigit(): 28 user_choice = int(user_choice) 29 if user_choice < len(product_list) and user_choice >=0: 30 p_item = product_list[user_choice] 31 if p_item[1] <= salary: 32 shopping_list.append(p_item) 33 salary-=p_item[1] 34 print('您購買了\033[32;1m[%s]\033[0m,當前賬戶余額為\033[32;1m%s元\033[0m'%(p_item[0],salary)) 35 else: 36 print('\033[41;1m您余額只有[%s元],還買個毛線\033[0m'% salary) 37 else: 38 print('\033[41;1m商品編號不存在,請輸入正確的商品編號\033[0m') 39 elif user_choice == 'q': # 程式退出 40 print('-------您的購物清單如下----------') 41 for p in shopping_list: 42 print(p) 43 print('您當前\033[31;1m余額\033[0m還有\033[31;1m[%s元]\033[0m'% salary) 44 exit() 45 else: 46 print('輸入有誤,請重新輸入')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/161027.html
標籤:Python
上一篇:Python:tkinter
