zoukankan      html  css  js  c++  java
  • 简单的购物车程序

    简单的购物车程序,用于练习,需求如下:

    启动程序后,让用户输入工资,然后打印商品列表
    允许用户根据商品编号购买商品
    用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    可随时退出,退出时,打印已购买商品和余额

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 # @Time    : 2017/9/14 16:46
     4 # @Author  : lichuan
     5 # @File    : shop_car.py
     6 
     7 product_list={
     8     "lenove":3000,
     9     "ASUS":4500,
    10     "MSI":2800,
    11     "HP":4200
    12 }
    13 FLAG_TAG=True
    14 count=0
    15 shop_car = {}  # 已购物的商品
    16 shop_list = []  # 购物车列表
    17 
    18 while FLAG_TAG:
    19     salary=input("input 'q' to exit,please input your salary:")
    20     if salary == 'q':
    21         FLAG_TAG=False
    22     elif salary.isdigit():
    23         salary=int(salary)
    24     else:
    25         print("input wrong,try again!")
    26         continue
    27     for p in product_list:
    28         print("%d,%s  %d" % (count, p, product_list[p]))
    29         shop_list.append(p)
    30         count += 1
    31 
    32     while FLAG_TAG:
    33         product_num=input("input 'q' to exit,please input the product no:")
    34         if product_num == 'q':
    35             FLAG_TAG=False
    36         elif product_num.isdigit() and int(product_num) < count:
    37             product_num=int(product_num)
    38             if salary >= product_list[shop_list[product_num]]:
    39                 if not shop_list[product_num] in shop_car:
    40                     shop_car[shop_list[product_num]]=1
    41                 else:
    42                     shop_car[shop_list[product_num]]+=1
    43                 salary = salary - product_list[shop_list[product_num]]
    44             else:
    45                 print("余额不足:请选别的商品!")
    46                 continue
    47         else:
    48             print("input 'q' to exit or input a number!")
    49             continue
    50         print("你购买的商品如下:")
    51         for s in shop_car:
    52             print("%s: %d" %(s,shop_car[s]))
    53         print("余额: %d" % salary)
  • 相关阅读:
    一个标准的类通常要拥有下面四个组成部分
    局部变量和成员变量的区别
    Java学习: 面向对象的使用与注意事项
    学习:内存的申请与释放
    虚拟内存与物理内存
    实现:win32实现EDIT控件读取和保存
    学习:生产者和消费者模式实现线程同步
    学习:线程互斥
    学习:多线程
    实现:服务程序增删
  • 原文地址:https://www.cnblogs.com/litzhiai/p/7522362.html
Copyright © 2011-2022 走看看