概述
python小作业4-商品购买
products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
print("题目products :%s" %(products))
j = 0
for product in products:
print(j, end="t")
print(product[0], end="t")
print(product[1])
j += 1
# -------------------------------------------------------------------------------
cart = [["iphone", 0, 6888], ["MacPro", 0, 14800], ["小米6", 0, 2499], ["Coffee", 0, 31], ["Book", 0, 60],
["Nike", 0, 699]]
# -------------------------------------------------------------------------------
# 1、展示商品:
print("商品列表如下:")
k = 0
for product in products:
print(k, end="t")
print(product[0], end="t")
print(product[1])
k += 1
# -------------------------------------------------------------------------------
# 2、商品购买循环:
while 1:
new_goods_index = input("请输入你要购买的商品编号,按q可退出购买界面:")
if new_goods_index == 'q':
break
elif (new_goods_index.isdigit() == False) or ((0 <= int(new_goods_index) <= 5) == False):
print("输入异常,请重新输入!")
continue
elif int(new_goods_index) == 0:
cart[0][1] += 1
elif int(new_goods_index) == 1:
cart[1][1] += 1
elif int(new_goods_index) == 2:
cart[2][1] += 1
elif int(new_goods_index) == 3:
cart[3][1] += 1
elif int(new_goods_index) == 4:
cart[4][1] += 1
elif int(new_goods_index) == 5:
cart[5][1] += 1
else:
continue
# -------------------------------------------------------------
# 3、打印用户购买的商品列表
print(cart)
s1 = "商品名"
s2 = "数量"
s3 = "单价"
s4 = "合计"
pay = 0
print("您购买的商品列表如下:")
print("%-30s%-30s%-30s%-30s" % (s1, s2, s3, s4))
for stuff in cart:
print("%-30s%-30s%-30s%-30s" % (stuff[0], stuff[1], stuff[2], stuff[1] * stuff[2]))
pay += stuff[1] * stuff[2]
print("您要支付的总金额为:%d元" % (pay))
最后
以上就是紧张滑板为你收集整理的python小作业4-商品购买的全部内容,希望文章能够帮你解决python小作业4-商品购买所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复