• 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
    

      

      

      

  • 相关阅读:
    RAM,ROM,内存还有硬盘到底有什么区别呢
    MySQL中的这个池子
    apk安装包介绍(下载安装,存储的位置,路径,可以对里面的文件进行修改吗)
    论文查询
    PID算法
    数组指针与指针数组
    2020 最佳开源项目出炉
    反射机制调用无参和有参方法以及调用私有方法
    CSS概述 CSS声明
    WEB概述
  • 原文地址:https://www.cnblogs.com/yongor/p/8856093.html
Copyright © 2020-2023  润新知