例題練習

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.pswp.cn/news/451489.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/451489.shtml
英文地址,請注明出處:http://en.pswp.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顯…