python-购物车程序

python-购物车程序,第1张

概述需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒用户可一直购买商品,也可随时退出,退出打印已购商品和余额解答:#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:youngshopping_list =

需求:

启动程序后,让用户输入工资,然后打印商品列表

允许用户根据商品编号购买商品

用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

用户可一直购买商品,也可随时退出,退出打印已购商品和余额


解答:

#!/usr/bin/env python# -*- Coding:utf-8 -*-# Author:youngshopPing_List = [    ('Watch',1200),    ('PC',11100),    ('Iphone',7800),    ('Bag',230)]shopped_List = []         #空列表,存放购买商品salary = input('请输入你的工资:')if salary.isdigit():      #判断输入的工资是否是数字    salary = int(salary)  #转    while True:        for index,shop in enumerate(shopPing_List):            print(index,shop)        user_choice = input("请输入商品编码:")        if user_choice.isdigit():            user_choice = int(user_choice)            if user_choice >= 0 and user_choice < len(shopPing_List):                if salary >= shopPing_List[user_choice][1]:                    shopped_List.append(shopPing_List[user_choice])                    salary = salary - shopPing_List[user_choice][1]                else:                    print('你的钱不够购买此商品,请更换其他商品,或者按q退出购买')            else:                print('你输入的商品编号不存在!')        elif user_choice == 'q':            print('你购买了%s商品,还剩下%s的钱'% (shopped_List,salary))            exit()        else:            print('输入的商品编号必须是数字!')else:        print('你输入薪水有误,必须是数字才行哟!')
总结

以上是内存溢出为你收集整理的python-购物车程序全部内容,希望文章能够帮你解决python-购物车程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1186416.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存