码迷,mamicode.com
首页 > 编程语言 > 详细

Python程序2——购物车小程序联系

时间:2017-10-31 12:51:34      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:lex   空间   UI   car   product   off   项目   class   user   


程序:购物车程序


需求:


  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额

 


1
stuff_list = ["1.water, price:500","2.beer, price:600","3.cola, price:700","4.phone, price:800","5.good water, price:900", #不用加反斜杠,直接在,下面加就好 2 "6.movie, price:1000"] 3 price_list = [500,600,700,800,900,1000] # 建议不用这种,直接price和stuff的合起来就好,可以用元组 4 5 # product_list = [(‘water, price:‘,500),(‘2.beer, price:‘,600),(‘3.cola, price:‘,700),("4.phone, price:",800) ……这样的形式就好 6 7 salary = input("Please input your salary:") 8 if salary.isdigit(): 9 salary = int(salary) 10 11 choice_list = [] 12 13 14 while True: #直接True 会好一点 15 print(stuff_list) 16 choice = int(input("Please input the goods number. If you want to quit,please input -1.")) 17 if choice == -1: 18 print("The goods you have bought are %s, and your money is \033[31;1m%s\033[0m left."%(choice_list,salary)) 19 break 20 else: 21 if price_list[choice-1] <= salary: #比较价格和剩余的金额 22 print("Goods %s is in your shopping car"%choice) 23 salary -= price_list[choice-1] 24 choice_list.append(stuff_list[choice-1]) 25 else: 26 print("Your money is not enough") 27 28 29 ‘‘‘ 30 31 product_list = [ #这种表述方式就比我的好,因为只用了一个空间,且不会因为顺序变化而导致商品编号出现问题,采用元组的形式也不会出现问题 32 (‘Iphone‘,5800), 33 (‘Mac Pro‘,9800), 34 (‘Bike‘,800), 35 (‘Watch‘,10600), 36 (‘Coffee‘,31), 37 (‘Alex Python‘,120), 38 ] 39 shopping_list = [] 40 salary = input("Input your salary:") 41 if salary.isdigit(): #判断是否为数字,然后就转化为int 42 salary = int(salary) 43 while True: 44 for index,item in enumerate(product_list): #高校地找出下标,enumerate这个不错,下标,项目内容 45 #print(product_list.index(item),item) 46 print(index,item) 47 user_choice = input("选择要买嘛?>>>:") 48 if user_choice.isdigit(): 49 user_choice = int(user_choice) 50 if user_choice < len(product_list) and user_choice >=0: #还有判断是否用户输入有误,是否在商品里面 51 p_item = product_list[user_choice] 52 if p_item[1] <= salary: #买的起 53 shopping_list.append(p_item) 54 salary -= p_item[1] 55 print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) ) #\033[31;1m%s\033[0m 这个就是变色,变成31红色32绿色33黄色的功能 56 else: 57 print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary) #41是背景的红色,42背景的绿色 58 else: 59 print("product code [%s] is not exist!"% user_choice) 60 elif user_choice == ‘q‘: 61 print("--------shopping list------") 62 for p in shopping_list: #好像直接print shopping_list 好像也可以用 63 print(p) 64 print("Your current balance:",salary) 65 exit() 66 else: 67 print("invalid option")‘‘‘

 

Python程序2——购物车小程序联系

标签:lex   空间   UI   car   product   off   项目   class   user   

原文地址:http://www.cnblogs.com/Ian-learning/p/7760424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!