例题练习

1,购物车

功能要求:
要求用户输入自己拥有总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
goods = [{"name": "电脑", "price": 1999},{"name": "鼠标", "price": 10},{"name": "游艇", "price": 20},{"name": "美女", "price": 998},
]
user_money = input("请输入您的资产>>")
user_money = int(user_money)
for item in goods:print(goods.index(item) + 1, item['name'], item['price'])
car = []
sum = 0
while 1:user_section = input("请输入您的选择的商品序号>>")if user_section.upper() == 'Q':if len(car) != 0:print("恭喜您购买成功,您一共消费%s元" % sum)breakif user_section.isdigit() and int(user_money) >= 0 and int(user_section) <= len(goods):if user_money < sum:print('商品价格为%s元,您的余额不足' % sum)breakuser_section = int(user_section) - 1car.append(goods[user_section]['name'])sum += goods[user_section]['price']else:print("非法输入")
View Code

2,计算BIM

# 1.创建并输出菜单, 菜单是不可变的. 所以使用元组

# 存储用户的信息 id: {'name':'名字', 'weight':体重, 'height':身高}
# 例如:目前有两个用户信息:1. 汪峰, 2. 章子怡
# 存储结构:
# {
# 1:{'name':'汪峰', 'weight':80, 'height':1.8, 'BMI':24.7},
# 2:{'name':'章子怡', 'weight':50, 'height':1.65, 'BMI':18.4}
# }

# 编号从1开始

# 体质指数(BMI)= 体重(kg)÷ (身高(m) x 身高(m))
# 体重的单位: KG
# 身高的单位: m
# 需求:首先。打印菜单,然后用户输入选择的菜单项
# 输入1:进入录入环节。用户需要录入:名字,身高,体重. while
# 由程序计算出BMI指数. 保存到bodies字典中. 第一个用户的id是1, 第二个是2, 以此类推
# 录入完毕后. 提示用户是否继续录入. 如果选择是, 则继续进行录入, 直到用户输入否. 则返回到主菜单
# 输入2: 进入查询环节, 提示用户输入要查询的人的id. 如果不存在,给与提示, 如果存在. 则显示出该用户的全部信息(名字,身高,体重,BMI)
# 然后提示用户是否继续查询. 如果选择是, 继续进行查询, 直到用户输入否, 返回主菜单
# 输入3: 进入删除环节, 提示用户输入要删除的人的id, 如果id不存在, 给与提示, 如果存在, 则执行删除操作. 并提示删除成功.
# 然后提示用户是否继续删除, 如果是, 继续让用户选择要删除的id, 直到用户输入否, 返回主菜单
# 输入4: 进入修改环节, 首先让用户输入要修改的人的id, 根据id查找用户信息, 如果不存在, 给与提示, 如果存在, 将用户原信息进行打印,
# 然后提示用户输入新的名字, 身高, 体重. 由程序重新计算BMI指数. 并将新的信息保存在bodies中. 同时给用户展示新的用户信息
# 然后提示用户是否继续修改, 如果是, 则继续要求用户输入id信息. 直到用户输入否, 返回主菜单.
# 输入5: 程序退出.
# 输入其他任何内容. 都予以提示不合法. 让用户重新进行输入
menus = ("1, 录入", "2, 查询", "3, 删除", "4, 修改", "5, 退出")
for item in menus:print(item)
bodies = {}
body_id = 1
flag = False
while 1:while flag:user_section_a = input('是/否')if user_section_a == '':dic = {"name": None, "weight": None, "height": None, "BMI": None}user_input_name = input('请输入您的名字>>')while 1:user_input_high = input('请输入您的身高>>')if not user_input_high.isdigit():print('请输入正确的身高')else:breakuser_input_high = int(user_input_high)while 1:user_input_weight = input('请输入您的体重>>')if not user_input_weight.isdigit():print("请输入正确的体重")else:breakuser_input_weight = int(user_input_weight)BMI = user_input_weight / (user_input_high ** 2)dic['name'] = user_input_namedic['height'] = user_input_highdic['weight'] = user_input_weightdic['BMI'] = BMIbodies[body_id] = dicbody_id += 1elif user_section_a == '':print(bodies)flag = Falseelse:print("请按照规定输入")user_section = input('please enter your section :')if user_section == '5':breakif user_section == '1':flag = Truedic = {"name": None, "weight": None, "height": None, "BMI": None}user_input_name = input('请输入您的名字>>')while 1:user_input_high = input('请输入您的身高>>')if not user_input_high.isdigit():print('请输入正确的身高')else:breakuser_input_high = int(user_input_high)while 1:user_input_weight = input('请输入您的体重>>')if not user_input_weight.isdigit():print("请输入正确的体重")else:breakuser_input_weight = int(user_input_weight)BMI = user_input_weight / (user_input_high ** 2)dic['name'] = user_input_namedic['height'] = user_input_highdic['weight'] = user_input_weightdic['BMI'] = BMIbodies[body_id] = dicbody_id += 1elif user_section == '2':while 1:user_cha = input('please enter your look id>>')if not user_cha.isdigit():print('输入错误,请重新输入')else:breakflag1 = Trueif int(user_cha) in bodies:print(bodies[int(user_cha)])else:print('NO the info')while flag1:user_section_b = input('是/否')if user_section_b == '':user_cha = input('please enter your look id>>')if int(user_cha) in bodies:print(bodies[int(user_cha)])else:print('NO the info')elif user_section_b == '':breakelse:print("请按照规定输入")elif user_section == '3':for item in bodies:print(item, bodies[item])while 1:user_shan = input('please enter your pop id')if not user_shan.isdigit():print("非法输入,请重新输入")else:breakuser_shan = int(user_shan)# bodies.pop(user_shan)if user_shan in bodies:bodies.pop(user_shan)body_id -= 1for key, value in bodies.items():if user_shan < key:a, b = key - 1, valuedel bodies[key]bodies[a] = belse:passelse:print("NO the id info")while 1:user_section_shan = input("是/否")if user_section_shan == "":for item in bodies:print(item, bodies[item])user_shan = input('please enter your pop id')user_shan = int(user_shan)if user_shan in bodies:bodies.pop(user_shan)body_id -= 1for key, value in bodies.items():if user_shan < key:a,b = key -1 , valuedel bodies[key]bodies[a] = belse:passelse:print('NO the id info')elif user_section_shan == '':breakelse:print("请按照规定输入")elif user_section == '4':for item in bodies:print(item, bodies[item])while 1:user_section_gai = input('please enter you change id>>')if not user_section_gai.isdigit():print("非法输入,请重新输入")else:breakuser_section_gai = int(user_section_gai)if user_section_gai in bodies:print('您要求改的内容为%s' % bodies[user_section_gai])print('请输入修改之后的信息')gai_name = input("Name:")while 1:gai_high = input("High:")if not gai_high.isdigit():print('非法输入,请重新输入')else:breakgai_high = int(gai_high)while 1:gai_weight = input("weight:")if not gai_weight.isdigit():print("非法输入,请重新输入")else:breakgai_weight = int(gai_weight)gai_BMI = gai_weight/(gai_high**2)bodies[user_section_gai] = {'name': gai_name, 'weight': gai_weight, 'high': gai_high, 'BMI': gai_BMI}else:print("NO the id info")while 1:user_section_gai_a = input('是/否')if user_section_gai_a == '':while 1 :user_section_gai = input('please enter you change id>>')if not user_section_gai.isdigit():print('非法输入,请重新输入')else:breakuser_section_gai = int(user_section_gai)if user_section_gai in bodies:print('您要求改的内容为%s' % bodies[user_section_gai])print('请输入修改之后的信息')gai_name = input("Name:")while 1:gai_high = input("High:")if not gai_high.isdigit():print("非法输入,请重新输入")else:breakgai_high = int(gai_high)while 1:gai_weight = input("weight:")if not gai_weight.isdigit():print("非法输入,请重新输入")else:breakgai_weight = int(gai_weight)gai_BMI = gai_weight / (gai_high ** 2)bodies[user_section_gai] = {'name': gai_name, 'weight': gai_weight, 'high': gai_high,'BMI': gai_BMI}else:print("NO the id info")elif user_section_gai_a == '':breakelse:print('请按照规定输入')else:print('输入错误')
View Code
 

 这个是修改版本,上面那个有瑕疵

menus = ("1, 录入", "2, 查询", "3, 删除", "4, 修改", "5, 退出")bodies = {}
body_id = 1while 1:for item in menus:print(item)user_section = input('please enter your section :')if user_section == '1':flag = Truewhile flag:dic = {"name": None, "weight": None, "height": None, "BMI": None}user_input_name = input('请输入您的名字>>')while 1:user_input_high = input('请输入您的身高>>')if not user_input_high.startswith(".") and not user_input_high.endswith(".") and user_input_high.count(".") == 1:breakelse:print("请输入正确的格式")user_input_high = float(user_input_high)while 1:user_input_weight = input('请输入您的体重>>')if not user_input_weight.startswith(".") and not user_input_weight.endswith(".") and user_input_weight.count(".") == 1 or user_input_weight.isdigit():breakelse:print("请输入正确的格式")user_input_weight = float(user_input_weight)BMI = user_input_weight / (user_input_high ** 2)dic['name'] = user_input_namedic['height'] = user_input_highdic['weight'] = user_input_weightdic['BMI'] = BMIbodies[body_id] = dicbody_id += 1while 1:user_section_a = input('是/否')if user_section_a == '':breakelif user_section_a == '':print(bodies)flag = Falsebreakelse:print("请按照规定输入")elif user_section == '2':flag2 = Truewhile flag2:user_cha = input('please enter your look id>>')if not user_cha.isdigit():print('输入错误,请重新输入')else:if int(user_cha) in bodies:print(bodies[int(user_cha)])else:print('NO the info')while 1:user_section_b = input('是/否')if user_section_b == '':breakelif user_section_b == '':flag2 = Falsebreakelse:print("请按照规定输入")elif user_section == '3':flag3 = Truewhile flag3:while 1:user_shan = input('please enter your pop id')if not user_shan.isdigit():print("非法输入,请重新输入")else:breakuser_shan = int(user_shan)if user_shan in bodies:bodies.pop(user_shan)body_id -= 1for key, value in bodies.items():if user_shan < key:a, b = key - 1, valuedel bodies[key]bodies[a] = belse:passelse:print("NO the id info")user_section_b = input('是/否')if user_section_b == '':breakelif user_section_b == '':flag3 = Falsebreakelse:print("请按照规定输入")elif user_section == '4':flag4 = Truewhile flag4:while 1:user_section_gai = input('please enter you change id>>')if not user_section_gai.isdigit():print("非法输入,请重新输入")else:breakuser_section_gai = int(user_section_gai)if user_section_gai in bodies:print('您要求改的内容为%s' % bodies[user_section_gai])print('请输入修改之后的信息')gai_name = input("Name:")while 1:gai_high = input("High:")if not gai_high.startswith(".") and not gai_high.endswith(".") and gai_high.count(".") == 1:breakelse:print("输入非法")gai_high = float(gai_high)while 1:gai_weight = input("weight:")if not gai_weight.startswith(".") and not gai_weight.endswith(".") and gai_weight.count(".") == 1 or gai_weight.isdigit():breakelse:print("非法输入")gai_weight = float(gai_weight)gai_BMI = gai_weight / (gai_high ** 2)bodies[user_section_gai] = {'name': gai_name, 'weight': gai_weight, 'high': gai_high, 'BMI': gai_BMI}else:print("NO the id info")while 1:user_section_b = input('是/否')if user_section_b == '':breakelif user_section_b == '':flag4 = Falsebreakelse:print("请按照规定输入")elif user_section == '5':breakelse:print('非法输入')
View Code

 

转载于:https://www.cnblogs.com/chenrun/p/9159122.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/451489.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

A端,B端,C端

A端是开发界面。即管理员所接触的界面。 B端是商家界面。即浏览器界面&#xff0c;依托于web界面。 C端是用户界面。即app的界面&#xff0c;用户所接触最为广泛的界面。

怎么用js动态 设置select中的某个值为选中项

可以使用javascript和jQuery两种实现方式 1&#xff1a;使用javascript实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org…

java常用简略语含义

首先这些对象都应用都是一些单词的简称&#xff0c;也是一种应用思想&#xff0c;故其他语言也可以使用&#xff0c;在 Java 里比较常见这些对象吧。下面来一一解释。 一、POJO&#xff08;Plain Ordinary Java Object&#xff09;。 简单而言&#xff0c;就是一个简单的对象&…

并行计算的强大

最近在处理一批数据&#xff0c;10的8次方&#xff0c;处理完毕大概要一个月&#xff0c;并且这个程序占用的CPU只有一个&#xff08;我从来没有注意到这个问题啊啊啊&#xff09;。 突然师兄提醒我可以把10的8次方条数据拆成10个10的7次方&#xff0c;作为10条任务并行处理&a…

Kubernetes集群(概念篇)

Kubernetes介绍 2013年docker诞生&#xff0c;自此一发不可收拾&#xff0c;它的发展如火如荼&#xff0c;作为一个运维如果不会docker&#xff0c;那真的是落伍了。 而2014年出现的kubernetes&#xff08;又叫k8s&#xff09;更加炙手可热&#xff0c;我想大部分人仅仅是听说过…

cannot resolve symbol xxxx问题

1.File->Invalidate Caches/Restart 清除缓存重启 2.还不行就maven -> Reinport

$(“#addLowForm“).serialize()同时提交其它参数的写法

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 1. 原本写法&#xff1a; 2. 不光传表单参数&#xff0c;还有别的参数的写法&#xff1a;

JAVA自学笔记25

JAVA自学笔记25 1、GUI 1&#xff09;图形用户接口&#xff0c;以图形的方式&#xff0c;来显示计算机操作的界面&#xff0c;更方便更直观 2&#xff09;CLI 命令行用户接口&#xff0c;就是常见的Dos&#xff0c;操作不直观 3&#xff09; 类Dimension 类内封装单个对象…

360——新式的流氓

360确实是一种新式的流氓。提供一些很多用户有用的工具&#xff0c;然后在同时&#xff0c;也提供一些流氓性的工具或者流浪性的推广方法&#xff0c;比如&#xff1a;对360浏览器&#xff0c;360桌面等工具&#xff0c;通过暗示性的广告语进行推广&#xff0c;而对于安装的诸多…

跳板机

现在一定规模互联网企业&#xff0c;往往都拥有大量服务器&#xff0c;如何安全并高效的管理这些服务器是每个系统运维或安全运维人员必要工作。现在比较常见的方案是搭建堡垒机环境作为线上服务器的入口&#xff0c;所有服务器只能通过堡垒机进行登陆访问&#xff0c;合格的堡…

Map是不是集合?

Map是不是集合&#xff1f; 一、起因 今天在一个群里跟几位朋友就“map是不是集合“”争执了起来&#xff1b;几位朋友一致认为map不是集合&#xff0c;他们说只有Collection接口下的才是集合&#xff0c;而我认为Collection和Map下的实现类都是集合类。二、发展 于是我开始在…

JAVA自学笔记08

JAVA自学笔记08 1、构造方法私有&#xff0c;外界就不能再创建对象 2、说明书的制作过程 1&#xff09;写一个工具类&#xff0c;在同一文件夹下&#xff0c;测试类需要用到工具类&#xff0c;系统将自动编译工具类&#xff1b;工具类的成员方法一般是静态的&#xff0c;因此…

创业,不能兼职

一直在寻找靠谱的技术人才加入自己的创业团队。这个靠谱&#xff0c;不仅是技术靠谱&#xff0c;还要有相同的价值观。价值观的概念也很广泛&#xff0c;除了人品&#xff0c;还有对一些涉及到做人做事最本质的一些理念要相同。最起码的一条是&#xff0c;你是不是真的想好了决…

Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

转载 http://www.cnblogs.com/skywang12345/p/3308852.html转载于:https://www.cnblogs.com/lizhouwei/p/9162251.html

@Controller和@RestController的区别

RestController注解相当于ResponseBody &#xff0b; Controller合在一起的作用。 1)如果只是使用RestController注解Controller&#xff0c;则Controller中的方法无法返回jsp页面&#xff0c;配置的视图解析器InternalResourceViewResolver不起作用&#xff0c;返回的内容就是…

spring AOP解说

1.aop切面编程就是在常规的执行java类中方法前或执行后加入自定义的方法。 比如你本来每天都去打酱油&#xff0c;去&#xff0c;打酱油&#xff0c;回。 现在我每天在你打酱油路上等着&#xff0c;你去打酱油的时候我打你一顿&#xff0c;回来的时候给你点糖果吃。 你根本不…

接口 EnvironmentAware

凡是被Spring管理的类&#xff0c;实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时&#xff0c;获取到系统环境变量和application配置文件中的变量。

简单安装ELK分析日志及使用心得

ELK是由Elasticsearch、Logstash、Kibana三个组件组成的。Elasticsearch&#xff1a;是ELK的核心插件&#xff0c;是一个基于Lucene的搜索服务器&#xff0c;它提供一个分布式多用户能力的全文搜索引擎&#xff0c;能够达到实时搜索&#xff0c;稳定&#xff0c;可靠&#xff0…

寄生式创业更容易成功

上次参加站长大会见识了不少创业团队和个人站长&#xff0c;他们中许多人都曾有过或正在过着苦逼的日子&#xff0c;不过我见到更多的还是他们风光的一面&#xff0c;在这次大会我见到了很多成功的创业团队&#xff0c;例如专门做微博营销的团队、依附于QQ空间的团队、专做腾讯…

JS单引号嵌套的问题,怎么改才能对呢!

JS单引号嵌套的问题&#xff0c;怎么改才能对呢&#xff01; https://zhidao.baidu.com/question/416584343.html document.getElementById(celbid).innerHTML<inputname""type"text"οnblur"celchangeb(celaid,celbid);">;这段代码是JS显…