栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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!!!')

结果

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对考高分网的支持。如果你想了解更多相关内容请查看下面相关链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/26987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号