黑松山资源网 Design By www.paidiu.com
要求:
启动程序后,让用户输入工资,然后打印出带有序号的商品列表
用户输入商品序号购买相应的商品,或者输入 ' q ' 退出购买界面
选择商品后,检查余额是否足够,够则直接扣款,不够则提示余额不足
用户每购买一件商品后,或者输入 ' q ' 退出购买界面后,提示:是否继续购买?(Y/N),实现多次购买
若用户购买了商品,打印出购买的商品列表,总金额,余额;若用户没买任何商品,打印:交易结束,购物失败
Readme:
运行程序,输入薪水,根据商品列表的序号选择购买的商品,可以选择多次购买,或者不购买
流程图:
代码:
# 简单的购物小程序 product_list = [ ['surface pro 4', 7800], ['dell xps 15', 12000], ['macbook', 12000], ['小米6', 2499], ['iphone7', 4600], ['坚果Pro', 1499] ] shopping_list = [] # 判断输入的薪水格式是否正确 while True: salary = input('\n请输入您的薪水:') if not salary.isdigit(): # 薪水不是数字,结束循环 print('\n输入格式有误!请重新输入...') continue break balance = salary = int(salary) print('\n-----------欢迎购买------------\n') # 生成带序号的商品列表 for index, item in enumerate(product_list): print(index, item) # 判断输入的序号是否符合要求 while True: while True: i = input('\n输入您要购买的商品序号,或输入 q 取消购买:') if i == 'q': # 输入 q 退出购买界面 while True: a = input('\n是否继续购买?(Y/N):') if a != 'n' and a != 'N' and a != 'y' and a != 'Y': print('\n输入格式有误,请重试...') continue elif a == 'y' or a == 'Y': # 继续购买 break else: # 购买完毕 if balance == salary: # 没有买任何东西 print('\n交易结束,购买失败...') exit() else: # 结算 print('\n您已成功购买以下商品:\n') for item in shopping_list: print(item) print('\n共消费金额 %d 元,余额 %d 元' % (salary - balance, balance)) exit() continue if not i.isdigit(): # 序号不是数字,结束循环 print('\n输入格式有误!请重新输入...') continue i = int(i) if i < 0 or i >= len(product_list): # 序号范围不正确,结束循环 print('\n此商品不存在,请重新输入...') continue break product = product_list[i] price = int(product[1]) # 判断余额是否充足,够就直接扣款,不够提醒 if price <= balance: balance -= price shopping_list.append(product_list[i]) print('\n您已成功购买 %s ,当前余额为 %d 元' %(product, balance)) else: print('\n购买失败,您的余额不足...') while True: a = input('\n是否继续购买?(Y/N):') if a != 'n' and a != 'N' and a != 'y' and a != 'Y': print('\n输入格式有误,请重试...') continue break if a == 'Y' or a == 'y': continue else: break if balance == salary: print('\n交易结束,购买失败...') exit() else: print('\n您已成功购买以下商品:\n') for item in shopping_list: print(item) print('\n共消费金额 %d 元,余额 %d 元' %(salary-balance, balance)) exit()
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 【原神】V5.0攻略 | 林尼攻略一图流
- 李翊君.1993-相思的烈酒【上华】【WAV+CUE】
- 古巨基.1998-LEO.KU(国)【千禧年代】【WAV+CUE】
- 郭子.2001-原来你什么都不想要创作集丫滚石】【WAV+CUE】
- 《使命召唤:黑色行动6》新预告公布!10月25日发售
- Atlus《暗喻幻想》媒体评分汇总:高分好评如潮!
- 2024金摇杆奖提名揭晓 《黑神话》角逐最佳视觉设计!
- 群星《新说唱2024 第3期 (上)》[320K/MP3][32.76MB]
- 群星《新说唱2024 第3期 (上)》[FLAC/分轨][95.38MB]
- 群星《新说唱2024 第3期 (下)》[320K/MP3][31.36MB]
- 幻兽帕鲁手游什么时候正式上线 最新消息一览
- 西普大陆BOSS位置盘点 解锁天启纪元玩法
- 西普大陆精灵进阶培养攻略 精灵养成指南
- dnf手游法控法系职业哪个强 dnf手游法控法系职业强度排行
- 魔兽世界血藤护目镜图纸在哪买 wlk血藤护目镜图纸购买位置介绍