03-python3.5-模拟购物车流程--更新追加细节注释功能

本文介绍了一个使用Python3.5编写的购物车模拟程序,详细展示了如何通过用户输入选择商品并实时更新购物车及剩余金额的过程。该程序包含商品列表、用户输入处理、余额计算和购物车展示等功能。

03-python3.5-模拟购物车流程--更新追加细节注释功能:

 

 

 

模拟购物车更新脚本:

 1 #!/usr/bin/env python
 2 #-*- coding: utf-8 -*-
 3 #__author__ = "XZ"
 4 
 5 product_list = [
 6     ('Iphone',5800),
 7     ('Mac Pro',8900),
 8     ('Watch',12000),
 9     ('Book',120),
10     ('Bike',800),
11     ('Coffee',31),
12 ]
13 
14 
15 shopping_list = []
16 salary = input("Input your salary: >>>")
17 print(product_list[0][0],type(product_list[0][0]))
18 print(product_list[0][1],type(product_list[0][1]))
19 if salary.isdigit():
20     salary = int(salary)
21     while True:
22         for index,item in enumerate(product_list):
23             print(index,item)
24         user_choice = input("请输入您需要购买商品的商品编号: >>>")
25         if user_choice.isdigit():
26             user_choice = int(user_choice)
27             if user_choice < len(product_list) and user_choice >= 0:
28                 p_item = product_list[user_choice]
29                 if p_item[1] <= salary:
30                     salary -= p_item[1]
31                     shopping_list.append(p_item)
32                     print("Add %s into your shopping cart , your current balance is \033[31;1m%s\033[0m" %(p_item,salary))
33                 else:
34                     print("\033[41;1m您的余额还剩%s啦,还买个毛线呢!\033[0m")
35             else:
36                 print("\033[41;1m您输入的商品编号不存在请输入正确的商品编号!\033[0m")
37         elif user_choice == 'q':
38             print("---------Shopping Cart List--------")
39             for p in shopping_list:
40                 print(p)
41             print("your currrent balance is %s" %(salary))
42             exit()
43         else:
44             print("\033[41;1mInvalid Optiong,请输入十进制格式的商品编号!\033[0m")

 

转载于:https://www.cnblogs.com/mypython2018/p/9757506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值