前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python实践:购物车

Python实践:购物车

作者头像
村雨遥
发布2019-09-09 17:17:18
8320
发布2019-09-09 17:17:18
举报
文章被收录于专栏:JavaPark

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/github_39655029/article/details/82747768

购物车程序

  • 要求如下图
  • 代码
代码语言:javascript
复制
# --*--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!!!')
  • 结果
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年09月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档