博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 基础之简单购物车小程序实现
阅读量:4306 次
发布时间:2019-06-06

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

购物车

all_list = [    ('mac',9000),    ('kindle',900),    ('tesla',800),    ('python',105),    ('bile',2000),]saving=input('输入资产:')#判断用户是否输入数字ishopping_car=[]  #if saving.isdigit():    saving=int(saving)    # for i in all_list:    while True:        for i,v in enumerate(all_list):  #自己添加序号类  ,分别赋值            # print(all_list.index(i)+1,i)            print(i+1,'>>>>>',v) #展示商品列表        choice=input('购买商品编号[退出q]:')        if choice.isdigit():            choice=int(choice)  #字符串转换数字            if choice > 0 and choice<=len(all_list): #判断输入的超出范围                p_item=all_list[choice-1]  #取出商品价格                if p_item[1] < saving: #将商品价格与用户输入价格做比较                    saving-=p_item[1] #将用户的输入的资产与商品价格相减,重新赋值给用户输入价格变量                    shopping_car.append(p_item) # 将用户购买商品添加到,这个元素里                else:                    print('余额不足%s元:'%saving)#如果钱不够就显示钱不够                print(p_item)            else:                print('编码不存在')#如果输入的不存在就直接显示不存在        elif choice=='q':# 输入的q表示退出            print('您购买如下商品')#并打印用户买的信息            for i in shopping_car:                print(i)            print('您还剩余%s元:'%saving)#在打印出用户剩余的钱            break

  测试

 

D:\python\python.exe D:/untitled/dir/for.py输入资产:50001 >>>>> ('mac', 9000)2 >>>>> ('kindle', 900)3 >>>>> ('tesla', 800)4 >>>>> ('python', 105)5 >>>>> ('bile', 2000)购买商品编号[退出q]:9编码不存在1 >>>>> ('mac', 9000)2 >>>>> ('kindle', 900)3 >>>>> ('tesla', 800)4 >>>>> ('python', 105)5 >>>>> ('bile', 2000)购买商品编号[退出q]:2('kindle', 900)1 >>>>> ('mac', 9000)2 >>>>> ('kindle', 900)3 >>>>> ('tesla', 800)4 >>>>> ('python', 105)5 >>>>> ('bile', 2000)购买商品编号[退出q]:3('tesla', 800)1 >>>>> ('mac', 9000)2 >>>>> ('kindle', 900)3 >>>>> ('tesla', 800)4 >>>>> ('python', 105)5 >>>>> ('bile', 2000)购买商品编号[退出q]:5('bile', 2000)1 >>>>> ('mac', 9000)2 >>>>> ('kindle', 900)3 >>>>> ('tesla', 800)4 >>>>> ('python', 105)5 >>>>> ('bile', 2000)购买商品编号[退出q]:q您购买如下商品('kindle', 900)('tesla', 800)('bile', 2000)您还剩余1300元:Process finished with exit code 0

  

转载于:https://www.cnblogs.com/rdchenxi/p/11088684.html

你可能感兴趣的文章
Java_基础语法之dowhile语句
查看>>
HDU 2175 汉诺塔IX
查看>>
PAT 甲级 1021 Deepest Root
查看>>
查找代码错误.java
查看>>
vc获取特殊路径(SpecialFolder)
查看>>
单例模式
查看>>
int(3)和int(11)区别
查看>>
201521123061 《Java程序设计》第十一周学习总结
查看>>
代码小思考
查看>>
Unity中的销毁方法
查看>>
ceph删除pool提示(you must first set the mon_allow_pool_delete config option to true)解决办法...
查看>>
2016-7-15(1)使用gulp构建一个项目
查看>>
文件上传案例——客户端和服务端套接字
查看>>
模拟BS服务器
查看>>
Lambda表达式——注重过程的编程思想
查看>>
网络通信和网络编程
查看>>
转换流
查看>>
序列化流
查看>>
线程池
查看>>
Junit单元测试
查看>>