zoukankan      html  css  js  c++  java
  • python学习day4--python基础--购物小程序

    '''
    购物小程序:
    用户启动时先输入工资
    用户启动程序后打印商品列表
    允许用户选择购买商品
    允许用户不断购买各种商品
    购买时检测余额是否够,如果够直接扣款,否则打印余额不足
    允许用户主动退出程序,退出时打印已购商品列表
    '''
    salary=input("input you salary:")
    if salary.isdigit():
    salary=int(salary)
    else:
    exit("invalid data type.....")
    welcome_msg='welcome to Shopping mall'.center(50,'-')
    print(welcome_msg)
    product_list=[
    ('Iphone',5888),
    ('Mac Air',8000),
    ('Mac Pro',9000),
    ('XiaoMi',19.9),
    ('Coffee',30),
    ('Tesla',820000),
    ('Bike',900),
    ('cloth',200)
    ]
    shop_car=[]
    exit_flag=False
    while not exit_flag:

    print('product list'.center(50, '-'))
    #for product_item in product_list:
    #p_name, p_price = product_item
    '''for p_name,p_price in product_list:
    print(p_name,p_price)'''
    for item in enumerate(product_list):
    index=item[0]
    p_name=item[1][0]
    p_price=item[1][1]
    print(index,p_name,p_price)
    user_choice=input("[q=quit,c=check]What do you want to buy?:")
    if user_choice.isdigit():#肯定是选择商品
    user_choice=int(user_choice)
    if user_choice<len(product_list):
    p_item=product_list[user_choice]
    if p_item[1]<salary:
    shop_car.append(p_item)#放入购物车
    salary -=p_item[1]#减钱
    print("Added [%s] into shop car,your current balance is [%s]"%(
    p_item,salary))
    else:
    print("your balance is [%s],can not afford this.."%salary)
    else:
    if user_choice=='q' or user_choice=="quit":
    print("purchased products as belows:".center(40,'*'))
    for item in shop_car:
    print(item)
    print("End".center(40,'*'))
    print("your balance is [%s]"%salary)
    exit_flag=True
    elif user_choice=='c' or user_choice=="check":
    print("purchased products as belows:".center(40,'*'))
    for item in shop_car:
    print(item)
    print("End".center(40,'*'))
    print("your balance is [%s]"%salary)
  • 相关阅读:
    Android SDK、NDK、JNI的简单介绍
    深入理解计算机系统—异常
    Jmeter3.1 使用及新增报告功能
    jmeter3.1连接数据库报错,ORA00923: 未找到要求的 FROM 关键字
    Jenkins插件、war下载地址
    jenkins自动打tag
    jenkins参数化构建过程
    Jmeter接口测试自动化(jmeter+ant+jenkins持续集成)
    既然选择开始就不会停下
    知识提升整体
  • 原文地址:https://www.cnblogs.com/gpdm/p/5830682.html
Copyright © 2011-2022 走看看