Python学习1,代码
看了好久的网上视频,今天尝试着写了一串代码:
1 _author_ = "Happyboy" 2 produce_list = [ 3 ('Iphone',5800), 4 ('Mac Pro',9800), 5 ('Bike',800), 6 ('Watch',10600), 7 ('Coffee',31), 8 ('Book',120) 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(produce_list): 16 print(index,item) 17 user_choice = input("选择要买吗?>>>:?") 18 if user_choice.isdigit(): 19 user_choice = int(user_choice) 20 if user_choice < len(produce_list) and user_choice >=0: 21 p_item = produce_list[user_choice] 22 if p_item[1] <= salary : #买得起 23 shopping_list.append(p_item) 24 salary -= p_item[1] 25 print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m"%(p_item,salary)) 26 else: 27 print("\033[41;1m你的余额只剩[%s]\033[0m"% salary) 28 else: 29 print("prodct code [%s] is not exist"% user_choice) 30 elif user_choice =="q": 31 print("--------shooping list-------") 32 for p in shopping_list: 33 print(p) 34 print("Your current balance:",salary) 35 exit() 36 else: 37 print("invalid option")
如果可以缩短,或者精炼的麻烦各位大牛帮我看下。。

浙公网安备 33010602011771号