今天写了个购物小程序,有的地方不够严谨,还需进一步完善,继续努力加油!
1 #!/usr/bin/env python 2 #Author: Tony Cab 3 4 users = (['cab','123'],['tony','234']) 5 shop_mall = [ 6 ['Thinkpad',5999], 7 ['Mac Proc',8000], 8 ['Mi note 2',3000], 9 ['Hua Wei',4000], 10 ['Suo lan tuo',300000], 11 ['Bicycle',3000], 12 ['Clothes',500], 13 ['Coffe',20], 14 ['Chocolate',10] 15 ] 16 17 shop_car = [] 18 shop_price = [] 19 total = 0 20 21 print('Who are you? Please login. Thanks!') 22 name = input('Account: ').strip() 23 passw = input('Password: ') 24 25 if [name,passw] in users: 26 print('Welcome %s to cabell shopping mall' %name) 27 else: 28 exit('Invalid account or password.') 29 30 salary = input('How much money do you have? ').strip() 31 if salary.isdigit(): sala = int(salary) 32 33 print('This is the list of goods. Please input the number of which do you want to buy.') 34 for item in enumerate(shop_mall): 35 index = item[0] 36 shop = item[1][0] 37 price = item[1][1] 38 print(index,':',shop,price) 39 40 41 flag = False 42 while not flag: 43 pri = 'This list \033[1;31;40m %s \033[0m is your goods. they take you \033[1;31;40m %s \ 44 \033[0m dollars' 45 num = input("Press 'q' to quit. Press number of good to buy. ").strip() 46 if num == 'q' or num == 'Q': 47 print(pri %(shop_car,total)) 48 flag = True 49 else: 50 numb = int(num) 51 shop_car.append(shop_mall[numb]) 52 total += shop_mall[numb][1] 53 if total > sala: 54 shop_car.pop() 55 total -= shop_mall[numb][1] 56 print(pri %(shop_car,total)) 57 exit('Your money is not enough. Please recharge.'.center(60,'*')) 58 else: 59 print(shop_car) 60 print(pri %(shop_car,total))
经过修改优化后:
1 #!/usr/bin/env python 2 #Author: Tony Cab 3 4 import getpass 5 6 flag = False 7 8 users = [['cab','123'],['tony','234']] #用户列表 9 10 new_acc = [] 11 shop_car = [] #定义变量 12 shop_price = [] 13 total = 0 14 15 16 while not flag: 17 acc = input('Do you have a account? if you have please press \'y\'. else press \'a\' to register. and \ 18 press \'q\' to quit.') 19 if acc == 'y': 20 break 21 elif acc == 'a': 22 uname = input('New Account: ').strip() #新用户注册 23 upassw = input('New Password: ') 24 upassd = input('Reput Password: ') 25 if upassw == upassd: 26 new_acc.append(uname) 27 new_acc.append(upassw) 28 users.append(new_acc) 29 print('Congratulations! register success.Please login') 30 break 31 else:continue 32 elif acc == 'q': 33 exit('Good Bye'.center(40,'-')) 34 else: 35 print('Please do it according introductions') 36 continue 37 38 print('Who are you? Please login. Thanks!') 39 name = input('Account: ').strip() #用户登录 40 passw = input('Password: ') 41 42 if [name,passw] in users: 43 print('Welcome %s to cabell shopping mall' %name) 44 else: 45 exit('Invalid account or password.') 46 47 salary = input('How much dollars do you have? ').strip() #输入可购买金额 48 if salary.isdigit(): sala = int(salary) 49 else: 50 exit('Error! Please input number.') 51 52 shop_mall = [ 53 ['Thinkpad',5999], 54 ['Mac Proc',8000], #购物商场商品列表 55 ['Mi note 2',3000], 56 ['Hua Wei',4000], 57 ['Suo lan tuo',300000], 58 ['Bicycle',3000], 59 ['Clothes',500], 60 ['Coffe',20], 61 ['Chocolate',10] 62 ] 63 64 print('This is the list of goods. Please input the number of which do you want to buy.') 65 for item in enumerate(shop_mall): 66 index = item[0] 67 shop = item[1][0] #待购商品列表 68 price = item[1][1] 69 print(index,':',shop,price) 70 71 72 73 while not flag: 74 pri = 'This list \033[1;31;40m %s \033[0m is your goods. they take you \033[1;31;40m %s \ 75 \033[0m dollars and left %s dollars' 76 num = input("Press 'q' to quit. Press \'s\' to reset which to remove from your shopping cart \ 77 Press number of good to buy. Press \'l\' to list your goods. Press 'e' to exit with out buy \ 78 anything").strip() 79 if num == 'q' or num == 'Q': 80 print(pri %(shop_car,total,sala-total)) 81 print('We hope you have a plesent everyday. See you later.') 82 flag = True 83 84 elif num.isdigit() and int(num) < len(shop_mall): 85 numb = int(num) 86 shop_car.append(shop_mall[numb]) 87 total += shop_mall[numb][1] #选择商品并购买 88 if total > sala: 89 shop_car.pop() 90 total -= shop_mall[numb][1] 91 print(pri %(shop_car,total,sala-total)) 92 print('Your money is not enough. Please recharge.'.center(60,'*')) 93 else: 94 print(shop_car) 95 print(pri %(shop_car,total,sala-total)) 96 elif num == 's': 97 for items in enumerate(shop_car): 98 inde = items[0] 99 goo = items[1][0] #已购商品 100 pric = items[1][1] 101 print(inde,':',goo,pric) 102 renum = input('Press the number of good to remove and press q to return shop_mall list') 103 if renum.isdigit() and int(renum) < len(shop_car): 104 renumb = int(renum) 105 total -= shop_car[renumb][1] #已购商品退货 106 shop_car.pop(renumb) 107 print(pri %(shop_car,total,sala-total)) 108 elif renum.isdigit() and int(renum) >= len(shop_car): 109 print('The number out of shopping number') 110 continue 111 else: 112 continue 113 elif num == 'l': 114 for list in enumerate(shop_car): 115 good_nam = list[1][0] 116 good_pri = list[1][1] #已购商品列表 117 print(good_nam,':',good_pri) 118 print('Total: %s' %total) 119 elif num.isdigit() and int(num) >= len(shop_mall): 120 print('The number out of shopping number') 121 continue 122 elif num == 'e': 123 total = 0 124 shop_car.clear() 125 print(pri %(shop_car,total,sala)) 126 else: 127 print('Please input according to the Useage') 128 continue