zoukankan      html  css  js  c++  java
  • 练习:购物车

    花了1个小时,但是这个代码看着好冗余。

    还有一些功能可以加,但是没想好怎么加?

    另外如果有1万个商品,这种方式肯定是不行的。。。先记录下,继续优化。

     1 # -*- coding:utf-8 -*-
     2 salary = 5000
     3 print'''
     4 ---------Commoditys List----
     5 1. iphone:%s
     6 2. mck book:%s
     7 3. python book:%s
     8 4. coffee:%s
     9 5. bicycle:%s
    10 '''%(9000,10000,90,32,1500)
    11 have_buy_commodity = []
    12 #计算选购商品与自己金额是否足够,打印余额不足
    13 # for i in range(2**25):
    14 #     i += 1
    15 #
    16 while True:
    17     # 询问购买什么
    18     want_buy_shop = int(input("你想买什么?1/2/3/4/5,退出点击6:"))
    19 
    20     if want_buy_shop == 1:
    21         difference0 = 9000 - salary
    22         print("您的余额不足,还差%s"%difference0)
    23     #可以的话,购买成功,并计算余额
    24     elif want_buy_shop == 2:
    25         difference1 = 10000 - salary
    26         print("您的余额不足,还差%s" % difference1)
    27 
    28     elif want_buy_shop == 3:
    29         difference2 = salary - 90
    30         salary = difference2
    31         if salary < 0:
    32             print("您的余额不足,不可购买此商品,请重新选择!")
    33             break
    34         else:
    35             print("购买成功,余额还有%s" % difference2)
    36             have_buy_commodity.append("python book")
    37 
    38     elif want_buy_shop == 4:
    39         difference3 = salary - 32
    40         salary = difference3
    41         if salary < 0:
    42             print("您的余额不足,不可购买此商品,请重新选择!")
    43             break
    44 
    45         else:
    46             print("购买成功,余额还有%s" % difference3)
    47             have_buy_commodity.append("coffee")
    48 
    49     elif want_buy_shop == 5:
    50         difference4 = salary - 1500
    51         salary = difference4
    52         if salary < 0:
    53             print("您的余额不足,不可购买此商品,请重新选择!")
    54             break
    55         else:
    56             print("购买成功,余额还有%s" % difference4)
    57             have_buy_commodity.append("bicycle")
    58 
    59     elif want_buy_shop > 5 and want_buy_shop != 6:
    60         print("输入有误!请输入给出的商品序号。")
    61 
    62     # 打印出已购买的商品
    63     elif want_buy_shop  == 6:
    64         print('您购买的商品如下:')
    65         print('******Have Buy Commoditys*******')
    66 
    67         print(have_buy_commodity)
    68 
    69         print('*******************************')
    70         print("您的余额为 %s"%salary)
    71         print("结束购物,欢迎下次光临!")
    72         break

     打印结果:

     1 ---------Commoditys List----
     2 1. iphone:9000
     3 2. mck book:10000
     4 3. python book:90
     5 4. coffee:32
     6 5. bicycle:1500
     7 
     8 你想买什么?1/2/3/4/5,退出点击6:1
     9 您的余额不足,还差4000
    10 你想买什么?1/2/3/4/5,退出点击6:2
    11 您的余额不足,还差5000
    12 你想买什么?1/2/3/4/5,退出点击6:3
    13 购买成功,余额还有4910
    14 你想买什么?1/2/3/4/5,退出点击6:4
    15 购买成功,余额还有4878
    16 你想买什么?1/2/3/4/5,退出点击6:5
    17 购买成功,余额还有3378
    18 你想买什么?1/2/3/4/5,退出点击6:5
    19 购买成功,余额还有1878
    20 你想买什么?1/2/3/4/5,退出点击6:4
    21 购买成功,余额还有1846
    22 你想买什么?1/2/3/4/5,退出点击6:3
    23 购买成功,余额还有1756
    24 你想买什么?1/2/3/4/5,退出点击6:2
    25 您的余额不足,还差8244
    26 你想买什么?1/2/3/4/5,退出点击6:1
    27 您的余额不足,还差7244
    28 你想买什么?1/2/3/4/5,退出点击6:5
    29 购买成功,余额还有256
    30 你想买什么?1/2/3/4/5,退出点击6:4
    31 购买成功,余额还有224
    32 你想买什么?1/2/3/4/5,退出点击6:3
    33 购买成功,余额还有134
    34 你想买什么?1/2/3/4/5,退出点击6:4
    35 购买成功,余额还有102
    36 你想买什么?1/2/3/4/5,退出点击6:3
    37 购买成功,余额还有12
    38 你想买什么?1/2/3/4/5,退出点击6:6
    39 您购买的商品如下:
    40 ******Have Buy Commoditys*******
    41 ['python book', 'coffee', 'bicycle', 'bicycle', 'coffee', 'python book', 'bicycle', 'coffee', 'python book', 'coffee', 'python book']
    42 *******************************
    43 您的余额为 12
    44 结束购物,欢迎下次光临!
    
    
  • 相关阅读:
    Day10
    Day9
    Day8
    Day 7
    Day-6
    java中的原子性
    java 原子性
    内存可见性
    JVM 常忘笔记
    JVM 解释执行 编译执行 JIT
  • 原文地址:https://www.cnblogs.com/aszeno/p/10155780.html
Copyright © 2011-2022 走看看