购物车

要求如下:

程序:购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒充值
  4. 可随时退出,退出时,打印已购买商品和余额
#!/usr/bin/env python
# -*- coding: utf-8 -*-
menu={
    'iphone':5000,
    'suoni':2000,
    'huawei':2500,
    'xiaomi':1500,
    'meizu':4500,
    'vivo':3500
}

shopping_cat=[]
salary=int(input('请输入工资:'))
while True:
    for product, price in menu.items():
        print('商品:%s 价格:%s' % (product, price))
    choice=input('请输入您要选择的选择的商品名称或者exit退出:').strip()
    if choice=='exit':break
    if choice not in menu:
        print('您选择的商品不存在')
        order=input('continue or exit:').strip()
        if order=='exit':
            break
        else:
            continue

    if salary > menu[choice]:
        shopping_cat.append(choice)
        salary=salary-menu[choice]
        print('购物车:%s' %shopping_cat)
        print('余额:%s' %salary)

    else:
        balance=(menu[choice]-salary)
        print('您的余额不足,还差%s' %balance)
        pay_choice=input('请选择充值pay或exit').strip()
        if pay_choice=='exit':
            break
        else:
            pay=int(input('请输入充值金额:'))
            salary=salary+pay
            print('充值金额为:%s,现在余额为:%s' %(pay,salary))

        y_n=input('请选择继续购买shop或者退出exit:')
        if y_n=='exit':
            break
        else:
            continue
购物车代码

流程图如下:

原文地址:https://www.cnblogs.com/lazyball/p/7611081.html