首页 技术 正文
技术 2022年11月8日
0 收藏 322 点赞 1,978 浏览 1623 个字

购物车功能如下:

1. 输入收入多少,购买商品

2. 打印购物清单,根据清单选择商品;

3. 结算,打印购物清单及总金额

 # -*- coding:utf-8 -*-
# LC goods=[[1,'iphone',5800],[2,'Mac Pro',12000],[3,'apple',30],[4,'egg',5],[5,'pig',2000]] #商品列表
salary = input('Please input your salary:') #输入薪资
shopping_cart = [] #购物车列表
shopping_money=[] #消费金额列表
salary=int(salary)
balance=int(salary) #消费余额,未开始消费等于薪资
shopping_total_money=0
while True:
for i in goods: #打印商品清单
print(i)
select = input("Please input your select,or input 'q' to quit:") #输入购买清单选项,按"q"退出
if select=="q":
if balance==salary: #判断是否购买了东西
print("Do not buy any thing")
break
else:
print("You have been bought below:")
for k in shopping_cart:
print(k)
print("Total Comsume:",shopping_total_money)
break
else:
select_sequence = int(select)
goods_max_sequence = int(goods[-1][0])
if select_sequence>goods_max_sequence: #如果输入序列号大于商品序列号,提示输入错误
print("------------Warning-------------")
print("Your selected is not correctly,please retry")
print("------------Warning-------------")
else:
goods_price = int(goods[select_sequence - 1][2])
if goods_price<=balance: #如果余额大于购买商品的金额,则加入购物车
shopping_cart.append(goods[select_sequence-1])
shopping_money.append(goods_price) #消费金额加入消费金额列表
shopping_total_money = sum(shopping_money) #计算商品总金额
shopping_total_money=int(shopping_total_money)
balance = salary - shopping_total_money #余额计算
else:
print('You have not enough money!') #余额不足,提示购买清单
for k in shopping_cart:
print(k)
print("Total Comsume:", shopping_total_money)
break

打印列表index,序号

 product_list = [["apple",180],["pig",1800],["rice",5],["bike",500]]
for item in product_list:
print(product_list.index(item),item) for index,item in enumerate(product_list):
print(index,item)

执行代码结果:

0 ['apple', 180]
1 ['pig', 1800]
2 ['rice', 5]
3 ['bike', 500]
0 ['apple', 180]
1 ['pig', 1800]
2 ['rice', 5]
3 ['bike', 500]
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,114
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,586
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,432
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,924