• python3----练习题(购物车)


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


    # 商品列表
    product_list = [
    ('Iphone8', 6888),
    ('MacPro', 14800),
    ('小米6', 2499),
    ('bike', 800),
    ('Coffee', 31),
    ('Nike Shoes', 799),
    ]
    # 购物车
    shopping_list = []
    salary = input("输入您的薪水:")
    if salary.isdigit(): # 检测字符串是否只由数字组成
    salary = int(salary)
    run_flag = True
    while run_flag:
    print(str.center('商品列表', 30, '-'))
    for k, v in enumerate(product_list):
    print('%s. %s %s' % (k, v[0], v[1]))
    choice = input("请输入想买商品的编号,退出请输入q:")
    if choice.isdigit():
    choice = int(choice)
    print(choice)
    if choice >= 0 and choice < len(product_list):
    item_price = product_list[choice]
    if item_price[1] <= salary: #买得起
    shopping_list.append(item_price) # 追加到购物车
    salary -= item_price[1]
    print("%s添加到购物车,您当前余额是:%d" % (item_price, salary))
    else:
    print('您当前余额不足')
    else:
    print('商品不存在')
    elif choice == 'q' or choice == 'Q':
    if len(shopping_list) > 0:
    print(str.center('您已购买以下商品', 30, '-'))
    for k, v in enumerate(shopping_list):
    print('%s. %s %s' % (k, v[0], v[1]))
    print("您当前余额:%d" % salary)
    run_flag = False
    
    
  • 相关阅读:
    python中os.listdir( )函数读取文件夹
    斐讯k2p 月光银 硬件版本A2-软件版本22.7.8.5 刷官改系统
    Ubuntu 16.04 换国内源
    pip和apt-get换源
    Python-OpenCV中的filter2D()函数
    Python-OpenCV中图像合并显示
    Python-OpenCV——Image Blurring(Image Smoothing)
    Python-OpenCV中的resize()函数
    删除Chrome地址栏记录中自动补全的网址
    Python中Numpy mat的使用
  • 原文地址:https://www.cnblogs.com/jonm/p/8262181.html
Copyright © 2020-2023  润新知