• Python学习第二天——购物车程序


    程序:购物车程序

    需求:

    1. 启动程序后,让用户输入工资,然后打印商品列表
    2. 允许用户根据商品编号购买商品
    3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
    4. 可随时退出,退出时,打印已购买商品和余额
    5. 如余额不足,可充值
     1 #coding=utf-8
     2 #Version:python 3.6.0
     3 #Tools:Pycharm 2017.3.2
     4 _date_ = '2018/4/16/016 14:50'
     5 _author_ = 'Hongyong'
     6 
     7 salary = int(input("Please input your salary: "))
     8 shopping_mart = []
     9 items = (["Huawei","",2800],
    10          ["Earphone","",300],
    11          ["Book","",80])
    12 msg_items = '''
    13 ----------items----------
    14 1. Huawei     ¥  2800
    15 2. Earphone   ¥  300
    16 3. Book       ¥  80
    17 -------------------------
    18 '''
    19 while True:
    20     print(msg_items)
    21     user_choice = input("Please choose goods: ")
    22     if user_choice.isdigit():
    23         p_index = int(user_choice)
    24         if p_index > 0 and p_index <= len(items):
    25             if salary >= items[p_index - 1][2]:
    26                 shopping_mart.append(items[p_index- 1])
    27                 salary -= int(items[p_index - 1][2])
    28                 print("You have bought {name} !".format(name=items[p_index - 1][1]))
    29                 print("Your balance is: ¥", salary)
    30             else:
    31                 print("Your balance is not enough! Please try sth else.")
    32                 recharge_ans = input("Do you want to recharge?")
    33                 if recharge_ans == "y":
    34                     recharge = int(input("Please input money: "))
    35                     print("Please wait for a while...")
    36                     salary += recharge
    37                     print("You have recharged successfully!")
    38                     print("And the balance is: ", salary, "now!")
    39         else:
    40             print("Invalid Choice!")
    41             print("You are supposed to enter from 1 to",len(items))
    42     elif user_choice == "q":
    43         print("---------shopping list------------")
    44         for index, ite in enumerate(shopping_mart):
    45             print(index + 1, ite)
    46         print("Your balance is: ¥", salary)
    47         print("Welcome your next coming!")
    48         print("Exit...")
    49         exit()
    50     else:
    51         print("Invalid Choice!")
    52         print("You are supposed to enter from 1 to", len(items))
    程序清单

      程序效果

    Please input your salary: 2
    
    ----------items----------
    1. Huawei     ¥  2800
    2. Earphone   ¥  300
    3. Book       ¥  80
    -------------------------
    
    Please choose goods: 1
    Your balance is not enough! Please try sth else.
    Do you want to recharge?y
    Please input money: 20000
    Please wait for a while...
    You have recharged successfully!
    And the balance is:  20002 now!
    
    ----------items----------
    1. Huawei     ¥  2800
    2. Earphone   ¥  300
    3. Book       ¥  80
    -------------------------
    
    Please choose goods: 1
    You have bought ¥ !
    Your balance is: ¥ 17202
    
    ----------items----------
    1. Huawei     ¥  2800
    2. Earphone   ¥  300
    3. Book       ¥  80
    -------------------------
    
    Please choose goods: 
    Invalid Choice!
    You are supposed to enter from 1 to 3
    
    ----------items----------
    1. Huawei     ¥  2800
    2. Earphone   ¥  300
    3. Book       ¥  80
    -------------------------
    
    Please choose goods: 2
    You have bought ¥ !
    Your balance is: ¥ 16902
    
    ----------items----------
    1. Huawei     ¥  2800
    2. Earphone   ¥  300
    3. Book       ¥  80
    -------------------------
    
    Please choose goods: 
    Invalid Choice!
    You are supposed to enter from 1 to 3
    
    ----------items----------
    1. Huawei     ¥  2800
    2. Earphone   ¥  300
    3. Book       ¥  80
    -------------------------
    
    Please choose goods: 3
    You have bought ¥ !
    Your balance is: ¥ 16822
    
    ----------items----------
    1. Huawei     ¥  2800
    2. Earphone   ¥  300
    3. Book       ¥  80
    -------------------------
    
    Please choose goods: q
    ---------shopping list------------
    1 ['Huawei', '¥', 2800]
    2 ['Earphone', '¥', 300]
    3 ['Book', '¥', 80]
    Your balance is: ¥ 16822
    Welcome your next coming!
    Exit...
    
    Process finished with exit code 0
    

      

      

      

  • 相关阅读:
    Linux 环境变量
    sharepoint webapp 部署注意点
    如何让html中的td文字只显示部分
    作业服务笔记
    HBase全分布安装配置
    CentOS Hadoop安装配置详细
    WCF访问超时:HTTP 请求已超过xx:yy分配的超时。为此操作分配的时间可能是较长超时的一部分。
    Apache Spark支持三种分布式部署方式 standalone、spark on mesos和 spark on YARN区别
    ext3文件系统挂载优化--HBase
    CentOS安装HBase
  • 原文地址:https://www.cnblogs.com/yongor/p/8856093.html
Copyright © 2020-2023  润新知