zoukankan      html  css  js  c++  java
  • 一个简单的购物车例子练习

     1 #coding:utf-8
     2 
     3 '''
     4 购物车:
     5 显示商品和购买价格。
     6 购入商品,加入购物车。
     7 付款,余额不足时提示,并可重新购买。
     8 '''
     9 
    10 shop_cart = []
    11 amount = 0
    12 wallet = 100
    13 commodity = {"大米":60,"香蕉":8,"饼干":12,"榴莲":17}
    14 
    15 print("商品名称:	金额(元):")
    16 for k,v in commodity.items():
    17     print("%s		%s"%(k,v))
    18 
    19 while True:
    20     print("
    结账请输入[B],重新购买请输入[R],退出请输入[Q]")
    21     shop = input("请输入购买商品名称:")
    22     if isinstance(shop,str):
    23         if shop == "Q":
    24             print("
    Goodbye Baby!!")
    25             break
    26         if shop == "R":
    27             print("
    I am back. baby!!")
    28             continue
    29         elif shop == "B":
    30             for i in shop_cart:
    31                 amount += commodity[i]
    32             if wallet < amount:
    33                 print("
    余额不足!!
    供需付款 %s (元),
    剩余金额 %s (元)"%(amount,wallet))
    34             else:
    35                 wallet -= amount
    36                 for i in shop_cart:
    37                     print("%s %s (元)" % (i, commodity[i]))
    38                 print("=================
    账单:%s (元)"%amount)
    39                 print("余额 %s (元)"%wallet)
    40         elif shop in commodity.keys():
    41             shop_cart.append(shop)
    42             print("
    购物车:")
    43             for n in shop_cart:
    44                 print("----------
    %s	%s (元)"%(n,commodity[n]))
    45         else:
    46             print("
    输入有误,请输入需要购买商品的名称!!!
    ")
    47     else:
    48         print("
    输入有误,请输入需要购买商品的名称!!!
    ")
  • 相关阅读:
    2016.11.30
    java韩顺平老师视频有需要可以留言
    UESTC 1425 Another LCIS
    hdu 3308 LCIS
    HDU 3308 LCIS 线段树区间更新
    poj crane
    poj1436 Horizontally Visible Segments
    编程习惯记录
    poj 3225 Help with Intervals
    UVA 1513 Movie collection
  • 原文地址:https://www.cnblogs.com/l729414559/p/6768747.html
Copyright © 2011-2022 走看看