模拟购物车购物过程python,Python基于数列实现购物车程序过程详解

要求

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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2020-06-06

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值