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

相关文章

Python实现以时间换空间的缓存替换算法

缓存是指可以进行高速数据交换的存储器,它先于内存与CPU交换数据,因此速度很快。缓存就是把一些数据暂时存放于某些地方,可能是内存,也有可能硬盘。 在使用Scrapy爬网站的时候,产生出来...

python 全局变量的import机制介绍

python 全局变量的import机制介绍

先把有问题的代码晒一下: IServer.py from abc import ABCMeta, abstractmethod print __name__ class ISer...

Python自动化测试ConfigParser模块读写配置文件

Python自动化测试ConfigParser模块读写配置文件 ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单。 直接上代码,不解释,不多说。 配...

python利用dlib获取人脸的68个landmark

(1) 单人脸情况 import cv2 import dlib path = "1.jpg" img = cv2.imread(path) gray = cv2.cvtColo...

跟老齐学Python之有点简约的元组

关于元组,上一讲中涉及到了这个名词。本讲完整地讲述它。 先看一个例子: >>>#变量引用str >>> s = "abc" >>>...