python小作业4-商品购买
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24products = [["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 # -------------------------------------------------------------------------------
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24# 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 # -------------------------------------------------------------
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 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-商品购买内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复