Python3 shopping_list.py

#!/bin/python
###产品列表
productList = [
    ['Iphone6', 5800],
    ['Macbook', 9000],
    ['Coffee', 32],
    ['Pythonbook', 80],
    ['Bicycle', 1500]
]
###创建一个购物列表
shoppingList=[]
###输入工资
salary = input("工资多少:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index, i in enumerate(productList): ####这里用的枚举方式
            print(index, i)
        userChoice=input("请输入产品序号或者q:")
        if userChoice.isdigit():
            userChoice=int(userChoice)
            if userChoice < len(productList) and userChoice >= 0:
                ### 产品序号范围
                productChoice=productList[userChoice]  ###选择产品
                if productChoice[1] < salary:
                    shoppingList.append(productChoice)
                    salary -= productChoice[1]
                    for s_index,s in enumerate(shoppingList):

                        print("----------选择商品信息----------")
                        print("%s,%s, 余额为:%d" %(s_index,s,salary))
                    print("-----------------END----------------------")
                else:
                    print("余额不足,")
                    for s_index,s in enumerate(shoppingList):

                        print("----------选择商品信息----------")
                        print("%s,%s,\n 余额为:%d" %(s_index,s,salary))
        elif userChoice=="q":
            print("结账")
            print("----------购买的商品信息----------")
            for s_index,i in shoppingList:
                print("%s,%s" % (s_index, i))
            print("余额为:%d" %salary ,"欢迎下次光临")
            break
        else:
            print("输入错误,请输入正确的产品序号")
    else:
        pass
else:
    print("输入错误")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石兴稳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值