购物车程序

要求

Python 购物车 - 图1

代码

  1. # --*--coding:utf-8--*--
  2. # Author: 村雨
  3. import pprint
  4. productList = [('Iphone 8', 10000),
  5. ('GTX2080', 8000),
  6. ('Z7KP7-GT', 6000),
  7. ('Mac pro', 15000),
  8. ('Honor 10', 2800),
  9. ('Iphone XR', 12000),
  10. ('Mi 8', 2999)
  11. ]
  12. shoppingList = []
  13. print('输入你的工资:')
  14. salary = input()
  15. if not salary.isdigit():
  16. print('请输入整数')
  17. else:
  18. salary = int(salary)
  19. while True:
  20. for index, item in enumerate(productList):
  21. print(index + 1, item)
  22. print('输入你要买的商品的序号:')
  23. userWant = input()
  24. if userWant.isdigit():
  25. userWant = int(userWant)
  26. if userWant <= len(productList) and userWant > 0:
  27. print('你要购买的是:', productList[userWant - 1][0])
  28. if salary >= productList[userWant - 1][1]:
  29. shoppingList.append(productList[userWant - 1][0])
  30. salary -= productList[userWant - 1][1]
  31. print('你已经购买了' + productList[userWant - 1][0] + ', 你的余额为 ' + str(salary))
  32. else:
  33. print('对不起,你的余额不足!请努力工作吧!')
  34. print('你当前所购买的商品为:')
  35. for brought in shoppingList:
  36. pprint.pprint(brought)
  37. print('你当前余额为:', salary)
  38. exit()
  39. else:
  40. print('你输入的商品序号有错,请重新输入')
  41. elif userWant == 'q':
  42. print('-----------Shopping List----------')
  43. for brought in shoppingList:
  44. pprint.pprint(brought)
  45. print('你的余额为 ', salary)
  46. exit()
  47. else:
  48. print('Invalid input!!!')

结果

Python 购物车 - 图2

Python 购物车 - 图3

Python 购物车 - 图4