Python实战购物车项目的实现参考

yipeiwu_com5年前Python基础

购物车程序

要求如下图

代码

# --*--coding:utf-8--*--
# Author: 村雨
import pprint
productList = [('Iphone 8', 10000),
        ('GTX2080', 8000),
        ('Z7KP7-GT', 6000),
        ('Mac pro', 15000),
        ('Honor 10', 2800),
        ('Iphone XR', 12000),
        ('Mi 8', 2999)
        ]
shoppingList = []
print('输入你的工资:')
salary = input()
if not salary.isdigit():
  print('请输入整数')
else:
  salary = int(salary)
  while True:
    for index, item in enumerate(productList):
      print(index + 1, item)
    print('输入你要买的商品的序号:')
    userWant = input()
    if userWant.isdigit():
      userWant = int(userWant)
      if userWant <= len(productList) and userWant > 0:
        print('你要购买的是:', productList[userWant - 1][0])
        if salary >= productList[userWant - 1][1]:
          shoppingList.append(productList[userWant - 1][0])
          salary -= productList[userWant - 1][1]
          print('你已经购买了' + productList[userWant - 1][0] + ', 你的余额为 ' + str(salary))
        else:
          print('对不起,你的余额不足!请努力工作吧!')
          print('你当前所购买的商品为:')
          for brought in shoppingList:
            pprint.pprint(brought)
          print('你当前余额为:', salary)
          exit()
      else:
        print('你输入的商品序号有错,请重新输入')
    elif userWant == 'q':
      print('-----------Shopping List----------')
      for brought in shoppingList:
        pprint.pprint(brought)
      print('你的余额为 ', salary)
      exit()
    else:
      print('Invalid input!!!')

结果

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

Python实现查找二叉搜索树第k大的节点功能示例

Python实现查找二叉搜索树第k大的节点功能示例

本文实例讲述了Python实现查找二叉搜索树第k大的节点功能。分享给大家供大家参考,具体如下: 题目描述 给定一个二叉搜索树,找出其中第k大的节点 就是一个中序遍历的过程,不需要额外的...

python基于socket实现网络广播的方法

本文实例讲述了python基于socket实现网络广播的方法。分享给大家供大家参考。具体实现方法如下: import socket, sys dest = ('<broadca...

在VS2017中用C#调用python脚本的实现

情景是这样的:在C#中调用python脚本进行post请求,python脚本中使用了requests包。 Python的开发环境我们有比较多的选择,pycharm、sublime tex...

django实现登录时候输入密码错误5次锁定用户十分钟

django实现登录时候输入密码错误5次锁定用户十分钟

在学习django的时候,想要实现登录失败后,进行用户锁定,切记录锁定时间,在网上找了很多资料,但是都感觉不是那么靠谱,于是乎,我开始了我的设计,其实我一开始想要借助redis呢,但是想...

python装饰器原理与用法深入详解

本文实例讲述了python装饰器原理与用法。分享给大家供大家参考,具体如下: 你会Python嘛? 我会! 那你给我讲下Python装饰器吧! Python装饰器啊?我没用过哎 以上...