Python实现购物车程序

 更新时间:2018年04月16日 16:40:45   作者:Hongory  
这篇文章主要为大家详细介绍了Python实现购物车程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了程序:Python购物车程序,具体内容如下

需求:

  • 启动程序后,让用户输入工资,然后打印商品列表
  • 允许用户根据商品编号购买商品
  • 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
  • 可随时退出,退出时,打印已购买商品和余额
  • 如余额不足,可充值 

代码:

#coding=utf-8
#Version:python 3.6.0
#Tools:Pycharm 2017.3.2
_date_ = '2018/4/16/016 14:50'
_author_ = 'Hongyong'

salary = int(input("Please input your salary: "))
shoppingmart = []
items = (["1","Huawei","¥",2800],
     ["2","Earphone","¥",300],
     ["3","Book","¥",80])
msg_items = '''
----------items----------
1. Huawei   ¥ 2800
2. Earphone  ¥ 300
3. Book    ¥ 80
-------------------------
'''
print(msg_items)
while True:
  shopindex = int(input("Please choose goods: "))
  if salary > items[shopindex-1][3]:
    shoppingmart.append(items[shopindex-1])
    salary -= int(items[shopindex-1][3])
    print("You have bought {name} !".format(name = items[shopindex-1][1]))
    print("Your balance is: ¥",salary)
    decision = input("Do you want to quit now?")
    print(msg_items)
  else:
    print("Your balance is not enough! Please try sth else.")
    recharge_ans = input("Do you want to recharge?")
    if recharge_ans == "y":
      recharge = int(input("Please input money: "))
      print("Please wait for a while...")
      salary += recharge
      print("You have recharged successfully!")
      print("And the balance is: ",salary,"now!")
    decision = input("Do you want to quit now?")
    print(msg_items)
  if decision == "q":
    break
  else:
    continue
print("You have bought: ",shoppingmart)
print("Your balance is: ¥",salary)
print("Welcome your next coming!") 

程序效果:

Please input your salary: 0
 
----------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: 30000
Please wait for a while...
You have recharged successfully!
And the balance is: 30000 now!
Do you want to quit now?
 
----------items----------
1. Huawei   ¥ 2800
2. Earphone  ¥ 300
3. Book    ¥ 80
-------------------------
 
Please choose goods: 1
You have bought Huawei !
Your balance is: ¥ 27200
Do you want to quit now?
 
----------items----------
1. Huawei   ¥ 2800
2. Earphone  ¥ 300
3. Book    ¥ 80
-------------------------
 
Please choose goods: 2
You have bought Earphone !
Your balance is: ¥ 26900
Do you want to quit now?q
 
----------items----------
1. Huawei   ¥ 2800
2. Earphone  ¥ 300
3. Book    ¥ 80
-------------------------
 
You have bought: [['1', 'Huawei', '¥', 2800], ['2', 'Earphone', '¥', 300]]
Your balance is: ¥ 26900
Welcome your next coming!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解如何用Python写个听小说的爬虫

    详解如何用Python写个听小说的爬虫

    在路上经常发现好多人都喜欢用耳机听小说,同事居然可以一整天的带着一只耳机听小说。本文就用Python爬虫实现下载听小说tingchina.com的音频,需要的可以参考一下
    2022-02-02
  • python中cPickle类使用方法详解

    python中cPickle类使用方法详解

    这篇文章主要为大家详细介绍了python中cPickle类的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • 在终端启动Python时报错的解决方案

    在终端启动Python时报错的解决方案

    这篇文章主要介绍了在终端启动Python时报错的解决方案,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-11-11
  • Python如何对接文心一言

    Python如何对接文心一言

    这篇文章主要为大家介绍了Python如何对接文心一言的操作实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • django执行原始查询sql,并返回Dict字典例子

    django执行原始查询sql,并返回Dict字典例子

    这篇文章主要介绍了django执行原始查询sql,并返回Dict字典例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • 在Python中绘制带有连接线的双饼图(操作代码)

    在Python中绘制带有连接线的双饼图(操作代码)

    这篇文章主要介绍了如何在Python中绘制带有连接线的双饼图,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • 如何用Python实现八数码问题

    如何用Python实现八数码问题

    这篇文章主要给大家介绍了关于如何用Python实现八数码问题的相关资料,八数码问题是一种经典的搜索问题,它的目标是将一个乱序的八数码序列变成一个有序的八数码序列,通常使用 A* 算法来解决,需要的朋友可以参考下
    2023-10-10
  • 如何在Django中添加没有微秒的 DateTimeField 属性详解

    如何在Django中添加没有微秒的 DateTimeField 属性详解

    这篇文章主要给大家介绍了关于如何在Django中添加没有微秒的 DateTimeField 属性的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • 使用Python实现获取文件详细信息

    使用Python实现获取文件详细信息

    Python提供了丰富的内置模块和函数,获取和操作文件的各种属性信息,比如大小、修改时间、权限以及路径等,本文将通过详细的示例代码展示如何使用Python中的os和os.path模块来获取文件属性信息,需要的可以参考下
    2023-12-12
  • python 实现波浪滤镜特效

    python 实现波浪滤镜特效

    这篇文章主要介绍了python 实现波浪滤镜特效的方法,帮助大家更好的利用python处理图片,感兴趣的朋友可以了解下
    2020-12-12

最新评论