zoukankan      html  css  js  c++  java
  • 购物车程序

    我们现在需要写这样一个购物车的程序。

    1. 程序开始后,显示购物商品列表,输入用户的工资
    2. 允许用户根据商品编号购买商品
    3. 在用户选择商品时检测余额是否足够,足够加入购物车,不足够就提醒余额不足
    4. 可随时退出,退出时打印购物车列表和余额
     1 product_list=[('iphone',5000),('whatch',500),('book',30),('bike',800),('banana',10)]
     2 shop_cart_list=[]
     3 salary=input("Input you salary:")
     4 if salary.isdigit():#如果salary是字符串
     5     salary=int(salary)
     6     while True:
     7         for index,item in enumerate(product_list):#enumerate();是将列表里的元素和列表里的元素次序按列表输出。
     8             #print(product_list.index(item),item)
     9             print(index,item)
    10         user_choice=input("what are you want to buy:")
    11         if user_choice.isdigit():
    12             user_choice=int(user_choice)
    13             if user_choice<len(product_list) and user_choice>=0:
    14                 p_item=product_list[user_choice]
    15                 if p_item[1]<salary:
    16                     shop_cart_list.append(p_item)
    17                     salary-=p_item[1]
    18                     print('Add %s into your shopping  cart,your balance is33[31;1m %s33[0m'%(p_item,salary))
    19                 else :
    20                     print('your balance is %s'%(salary))
    21             else:
    22                 print('product code [%s] is not exiting'%user_choice)
    23         elif user_choice=='q':
    24             print('exit....')
    25             print('-----------shopp cart list----------')
    26             for p in shop_cart_list:
    27                 print(p)
    28             print('your balance is %s'%salary)
    29             break
    30         else:
    31             print('invalid option')

    以上代码就是可以满足以上需求的购物车程序。

  • 相关阅读:
    textArea中的placeholder属性不起作用
    文件超出大小,进度条监听一直死循环一般的报错
    AJAX提交表单,上传出错的国际化信息无法显示在jsp页面上
    使用ajax提交表单,页面还是会自动刷新
    Springboot + vue 前后端分离学习
    Spring复习
    AJAX学习
    JWT以及相干实践
    动态sql语句MyBats
    SSH项目整合---项目环境搭建
  • 原文地址:https://www.cnblogs.com/fromzore/p/7868319.html
Copyright © 2011-2022 走看看