码迷,mamicode.com
首页 > 其他好文 > 详细

购物车

时间:2017-09-29 16:46:42      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:名称   bsp   int   alt   存在   app   env   exit   display   

要求如下:

程序:购物车程序

需求:

  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
购物车代码

流程图如下:

技术分享

 

购物车

标签:名称   bsp   int   alt   存在   app   env   exit   display   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!