Python简单循环应用

snowdream的Python简单循环应用(二)

product_list = [{‘name’: ‘苹果’, ‘price’: 10},
{‘name’: ‘榴莲’, ‘price’: 30},
{‘name’: ‘草莓’, ‘price’: 20},
{‘name’: ‘菠萝’, ‘price’: 15}]

超市买水果功能分析
(1)输入自己所有的钱。
(2)展示商品的序号,名称及其价格。
(3)输入要买商品的序号。
(4)输入要买商品的数量。
(5)购物车中显示购买的水果名称及其对应的数量和剩余钱。
(6)如果序号输入有误就提示用户重新输入。
(7)如果钱不够了提示用户钱不够,并且退出程序。

本问题解决方法代码如下:

product_list = [{'name': '苹果', 'price': 10},
              {'name': '榴莲', 'price': 30},
              {'name': '草莓', 'price': 20},
              {'name': '菠萝', 'price': 15}]
mone=int(input("请输入金额:"))
rmone=0
c=0
a=0
while a<len(product_list):
    product_list[a]["id"]=a+1
    a+=1
print(product_list)
while True:
    if c==1:
        print("你的余额不足,无法购买商品")
        break
    else:
        id = int(input("输入商品的序号:"))
        sum = int(input("输入商品的数量:"))
        b = 0
        if id > len(product_list):
            print("没有{}商品".format(id))
            continue
        while b <= len(product_list):
            if b == id:
                print("你选中{0}号商品:{1},你要买{1}的数量{2}斤".format(id, product_list[id - 1]["name"], sum))
                rmone = rmone + ((product_list[id - 1]["price"]) * sum)
                print("你已经选购{}元的商品".format(rmone))
                e = mone - rmone
                print("你剩余的钱{}元".format(e))
            b += 1
    if e <= 0:
        c = 1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值