博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Python语言实现的购物车程序<入门小白>
阅读量:6956 次
发布时间:2019-06-27

本文共 1429 字,大约阅读时间需要 4 分钟。

1.需求:     1:启动程序后,让用户输入工资,然后打印商品列表     2:允许用户根据商品编号购买商品     3:用户选择商品后,检测余额是否够,够就直接付款,不够就提醒     4:可随时推出,推出时,打印已购买商品和余额

2.代码如下:

__author__ = "B J"product_list = [    ('Iphone',5800),    ('Mac Pro',9800),    ('Bike',800),    ('Watch',10600),    ('Coffee',31),]shopping_list = []salary = input("Input your salary:")if salary.isdigit():    salary = int(salary)    while True:        for index,item in enumerate(product_list):            #print(product_list.index(item),item)            print(index,item)        user_choice = input("选择要买嘛?>>>:")        if user_choice.isdigit():            user_choice = int(user_choice)            if user_choice < len(product_list) and user_choice >=0:                p_item = product_list[user_choice]                if p_item[1] <= salary: #买的起                    shopping_list.append(p_item)                    salary -= p_item[1]                    print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )                else:                    print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary)            else:                print("product code [%s] is not exist!"% user_choice)        elif user_choice == 'q':            print("--------shopping list------")            for p in shopping_list:                print(p)            print("Your current balance:",salary)            exit()        else:

 

转载于:https://www.cnblogs.com/jb9527/p/10012293.html

你可能感兴趣的文章
20个优秀手机界面扁平化设计,让你一秒看懂扁平化
查看>>
从百度的PPT文化看程序员晋升
查看>>
Python测试登录功能
查看>>
mysql 创建高性能索引
查看>>
babel插件入门-AST(抽象语法树)
查看>>
分布式ID
查看>>
K8S 1.12大特性最快最深度解析:通过ComponentConfig更轻松安装和升级
查看>>
DailyTask-Kano模型量化需求笔记
查看>>
webpack 4教程(一) 入门篇搭建基本开发环境
查看>>
用Kotlin实现极简回调
查看>>
微服务开发中的数据架构设计
查看>>
2017年排名Top 100的Java类库——在分析了259,885份源码之后得出的结论
查看>>
人人都会设计模式---建造者模式--Builder
查看>>
知识小集 2018 下半年文章汇总及年终总结
查看>>
一个 Chrome XSS Filter Bypass 的分析
查看>>
在sketch中巧绘donut chart
查看>>
使用Kotlin高效地开发Android App(三)
查看>>
Android Native Crash 收集
查看>>
Andoid鬼点子 近期项目总结(2) 日历
查看>>
原声写法操作table
查看>>