• Python程序2——购物车小程序练习


    
    

    程序:购物车程序

    
    

    需求:

    
    
    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 33[31;1m%s33[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 33[31;1m%s33[0m" %(p_item,salary) ) #33[31;1m%s33[0m 这个就是变色,变成31红色32绿色33黄色的功能 56 else: 57 print("33[41;1m你的余额只剩[%s]啦,还买个毛线33[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")'''
  • 相关阅读:
    C#日期与时间
    使用IIS部署WebDAV
    永久有效,最新IntelliJ IDEA 2021.1.1激活破解教程,亲测有效
    LNC(宝元)机床的IP设置和数据采集
    KND(凯恩帝)机床的IP设置
    国密算法SM1-SM4简介
    WPF中根据数据展示不同UI控件的做法
    WPF中ListView滚动到当前行的几种方法
    WPF中对于异步返回的绑定
    .net类库名称空间冲突怎么办?
  • 原文地址:https://www.cnblogs.com/Ian-learning/p/7760424.html
Copyright © 2020-2023  润新知