【Python基础】day2——列表、元组练习题(简易购物车程序)

需求场景描述:

购物车程序
----------- Welcome to BUY_CENTLE ------------
         salary:5000
         1.iPhone 11    6000
         2.mac book     9000
         3.coffee       30
         4.oython book  80
         5.bicycle      1500
-----------------------------------------------
要求显示:
提示符>>>:1
     返回   余额不足,-1000
     >>>:5
     已加入购物车,当前余额3500
     >>>:quit  退出
     退出的同时返回:
         您已购买以下商品
         bicycle   1500
         coffee    30

        您的余额为:2750
        欢迎下次光临!!!

 代码实现:

#购物车信息屏显
shop_msg='''
----------- Welcome to BUY_CENTLE ------------
        1.iPhone 11    6000
        2.mac book     9000
        3.coffee       30
        4.python book  80
        5.bicycle      1500
-----------------------------------------------
'''
print(shop_msg)
#定义购物车列表
shopping_cart=[]

#逻辑代码
user_salary=int(input("Please input your salary : "))
user_choose=input(">>>: ")
while user_choose != 'quit':
    if user_choose == '1':
        if user_salary < 6000:
            print("余额不足",user_salary-6000)
        else:
            print("已加入购物车,当前余额",user_salary-6000)
            user_salary=user_salary-6000
            shopping_cart.append('iPhone 11')
    elif user_choose == '2':
        if user_salary < 9000:
            print("余额不足",user_salary-9000)
        else:
            print("已加入购物车,当前余额",user_salary-9000)
            user_salary=user_salary-9000
            shopping_cart.append('mac book')
    elif user_choose == '3':
        if user_salary < 30:
            print("余额不足",user_salary-30)
        else:
            print("已加入购物车,当前余额",user_salary-30)
            user_salary=user_salary-30
            shopping_cart.append('coffee')
    elif user_choose == '4':
        if user_salary < 80:
            print("余额不足",user_salary-80)
        else:
            print("已加入购物车,当前余额",user_salary-80)
            user_salary=user_salary-80
            shopping_cart.append('python book')
    elif user_choose == '5':
        if user_salary < 1500:
            print("余额不足",user_salary-1500)
        else:
            print("已加入购物车,当前余额",user_salary-1500)
            user_salary=user_salary-1500
            shopping_cart.append('bicycle')
    else:
        print("您输入有误,请重新输入!")
    user_choose = input(">>>: ")
else:
    print('''
    ---- 您已购买一下商品 ----
    
    ''',
          shopping_cart,
          '''
    
    ------ 欢迎下次光临 ------
    ''')

从上面的代码中可以看出,出现大量的重复代码,所以我们使用调用函数,减少代码量,示例:

#!/usr/bin/python

#定义余额判断函数
def yue(user_salary_def,goods_price,goods):
    if user_salary_def < goods_price:
        print("余额不足", user_salary_def - goods_price)
    else:
        print("已加入购物车,当前余额", user_salary_def - goods_price)
        global user_salary
        user_salary = user_salary_def - goods_price
        shopping_cart.append(goods)

# 购物车信息屏显
shop_msg = '''
----------- Welcome to BUY_CENTLE ------------
        1.iPhone 11    6000
        2.mac book     9000
        3.coffee       30
        4.python book  80
        5.bicycle      1500
-----------------------------------------------
'''
print(shop_msg)
# 定义购物车列表
shopping_cart = []

# 逻辑代码
user_salary = int(input("Please input your salary : "))
user_choose = input(">>>: ")
while user_choose != 'quit':
    if user_choose == '1':
        yue(user_salary,6000,'iPhone 11')
    elif user_choose == '2':
        yue(user_salary,9000,'mac book')
    elif user_choose == '3':
        yue(user_salary,30,'coffee')
    elif user_choose == '4':
        yue(user_salary,80,'python book')
    elif user_choose == '5':
        yue(user_salary,1500,'bicycle')
    else:
        print("您输入有误,请重新输入!")
    user_choose = input(">>>: ")
else:
    print('''
    ---- 您已购买一下商品 ----

    ''',
          shopping_cart,
          '''

    ------ 下次多买点,小气鬼 ------
    ''')

执行结果示例:

----------- Welcome to BUY_CENTLE ------------
        1.iPhone 11    6000
        2.mac book     9000
        3.coffee       30
        4.python book  80
        5.bicycle      1500
-----------------------------------------------

Please input your salary : 10000
>>>: 1
已加入购物车,当前余额 4000
>>>: 5
已加入购物车,当前余额 2500
>>>: 4
已加入购物车,当前余额 2420
>>>: quit

    ---- 您已购买一下商品 ----

     ['iPhone 11', 'bicycle', 'python book'] 

    ------ 下次多买点,小气鬼 ------
    

Process finished with exit code 0

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值