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

yipeiwu_com6年前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设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

使用pytorch搭建AlexNet操作(微调预训练模型及手动搭建)

使用pytorch搭建AlexNet操作(微调预训练模型及手动搭建)

本文介绍了如何在pytorch下搭建AlexNet,使用了两种方法,一种是直接加载预训练模型,并根据自己的需要微调(将最后一层全连接层输出由1000改为10),另一种是手动搭建。 构建模...

python读取word文档的方法

本文实例讲述了python读取word文档的方法。分享给大家供大家参考。具体如下: 首先下载安装win32com from win32com import client as wc...

Python FTP文件定时自动下载实现过程解析

这篇文章主要介绍了Python FTP文件定时自动下载实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一、需求:   某数...

python根据时间获取周数代码实例

时间 时间和周数 import time import datetime # 获取今天是第几周 print(time.strftime('%W')) # 获取当前是周几(0-6,0...

Python中不同进制的语法及转换方法分析

本文实例讲述了Python中不同进制的语法及转换方法。分享给大家供大家参考,具体如下: 不同进制的书写方式 ① 八进制(Octal) 0o377 ② 十六进制(Hex) 0xFF ③ 二...