ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用python的数列相关的知识,书写购物车程序

2020-06-05 19:58:27  阅读:278  来源: 互联网

标签:haveGoods python clothes 用户 购物车 print input balance 数列


要求

1、启动程序后让用户输入余额,并打印商品列表
2、用户通过输入编号购买商品
3、用户选择商品购买后,根据余额判断成功或者失败,给出对应提示
4、可以随时退出,退出后打印账号余额以及购买的商品列表

构思

1、首先,用户余额需要进行存储,用户购买的物品需要进行存储在数组中
2、用户购买成功后,将购买的物品放入物品集合,并用总金额减去余额
3、如果失败,给出失败提示,并打印余额
4、用户选择继续后,无论成功失败,都可以继续购买

代码

# 用户输入工资
balance = int(input("Please input balance:"))
# 定义衣服的数组
clothes = [["pants",100],["T-shirt",50],["skirt",20]]
# 个人所得,包括金钱和获取的物品
haveGoods = [balance,[]]
flag = True
while flag:
# 打印衣服列表
print("The clothes list is as follows")
print("______clothesList______")
i = 1;
for c in clothes:
print('The number:',i,":",c)
i += 1

# 用户输入商品编号
code = int(input("Please choose the number:"))
# 判断钱是否够用
if clothes[code-1][1] <= haveGoods[0]:
# 在自己的购物清单中加入已购物品
haveGoods[1].append(clothes[code-1])
# 减去花费的金钱
haveGoods[0] -= clothes[code-1][1]
print("You have successfully purchased!")
print("Your account balance is:",haveGoods[0])
else:
print("Your account balance is insufficient!")
print("Your account balance is:",haveGoods[0])
judge = input("You can press any button to continue,or input 'n' to leave:")
if judge == "n":
flag = False
print("Your account balance is:",haveGoods[0])
print("Your shopping list is as follows:")
print("______clothesList______")
for h in haveGoods[1]:
print(h)


标签:haveGoods,python,clothes,用户,购物车,print,input,balance,数列
来源: https://www.cnblogs.com/mcjhcnblogs/p/13051626.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有