• 简单的购物车程序


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

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

     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)
  • 相关阅读:
    实现随机颜色
    为网站实现一个验证码
    vue.js帐号,密码,邮箱和移动手机号码正则验证
    从网址中截去主机名和参数
    vue.js判断网址参数是否有效
    创建windows service
    vue.js axios call api example
    vue.js mouse over change the image
    jQuery接收url的参数
    ms sql server排序
  • 原文地址:https://www.cnblogs.com/litzhiai/p/7522362.html
Copyright © 2020-2023  润新知