题目,有一个动植物对应的字典内容如下,请根据用户输入列出对应的动物或者植物,实现字典三级菜单功能
dic = {
"植物":
{"草本植物":
["牵牛花","瓜叶菊","翠菊","冬小麦","甜菜"],
"水本植物":
["乔木","灌木","半灌木","如松","杉","樟"],
"水生植物":
["荷花","千屈菜","菖蒲","黄菖蒲","水葱","再力花","梭鱼草"]},
"动物":
{"两栖动物":
["山龟","山鳖","石蛙","娃娃鱼","蟾蜍","龟","鳄鱼","蜥蜴","蛇"],
"禽类":
["雏鸡","原鸡","长命鸡","昌国鸡","斗鸡","长尾鸡","乌骨鸡"],
"哺乳类动物":
["虎","狼","鼠","鹿","貂","猴","嫫","树懒","斑马","狗"]}}
vim day13-1.py
dic ={"植物":
{"草本植物":
["牵牛花","瓜叶菊","翠菊","冬小麦","甜菜"],"水本植物":
["乔木","灌木","半灌木","如松","杉","樟"],"水生植物":
["荷花","千屈菜","菖蒲","黄菖蒲","水葱","再力花","梭鱼草"]},"动物":
{"两栖动物":
["山龟","山鳖","石蛙","娃娃鱼","蟾蜍","龟","鳄鱼","蜥蜴","蛇"],"禽类":
["雏鸡","原鸡","长命鸡","昌国鸡","斗鸡","长尾鸡","乌骨鸡"],"哺乳类动物":
["虎","狼","鼠","鹿","貂","猴","嫫","树懒","斑马","狗"]}}#循环加序号打印key
li = [] #定义一个空列表,用来存放第一次循环的key及本题的"动物","植物"
for i,v in enumerate(dic,1): #for循环,使用enumerate方法生成序号,序号是从1开始
print(i,v) #打印序号加第一个key 分别为1 动物 2 植物 由于字典是无序的这里序号可能会有变化
li.append(v) #将key作为列表的元素追加至列表li此时 li = ["动物","植物"]同样顺序可能有变
u_c = input(">>>") #1 #等待用户输入选择输入对应的大类如输入1代表选择了动物2代表选择了植物,这里为了方便假设输入为数字
u_c = int(u_c) #将输入的字符串转换成整数
li1 = [] #定义一个空列表用来存放用户选择大类下的小分类
for i,v in enumerate(dic[li[u_c-1]],1): #for循环根据用户输入假如输入1选择动物则迭代动物这个key下面的分类
print(i,v) #列出对应的序号及分类
li1.append(v) #假如选择的是动物li1 = ["禽类","哺乳类动物","两栖动物"]
u_c1 = input(">>>>") #提示用户输入对应的数字
u_c1 =int(u_c1)for i in dic[li[u_c-1]][li1[u_c1-1]]: #根据用户输入列出对应的三级分类项,假如选择了禽类对应的数字列出所有禽类
print (i)
以上只允许一次性输入,下面加入无限循环可以输入b回退输入q退出
vim day13-2.py
dic ={"植物":
{"草本植物":
["牵牛花","瓜叶菊","翠菊","冬小麦","甜菜"],"水本植物":
["乔木","灌木","半灌木","如松","杉","樟"],"水生植物":
["荷花","千屈菜","菖蒲","黄菖蒲","水葱","再力花","梭鱼草"]},"动物":
{"两栖动物":
["山龟","山鳖","石蛙","娃娃鱼","蟾蜍","龟","鳄鱼","蜥蜴","蛇"],"禽类":
["雏鸡","原鸡","长命鸡","昌国鸡","斗鸡","长尾鸡","乌骨鸡"],"哺乳类动物":
["虎","狼","鼠","鹿","貂","猴","嫫","树懒","斑马","狗"]}}#循环加序号打印key
li = [] #定义一个空列表,用来存放第一次循环的key及本题的"动物","植物"
go =Truewhilego:for i,v in enumerate(dic,1): #for循环,使用enumerate方法生成序号,序号是从1开始
print(i,v) #打印序号加第一个key 分别为1 动物 2 植物 由于字典是无序的这里序号可能会有变化
li.append(v) #将key作为列表的元素追加至列表li此时 li = ["动物","植物"]同样顺序可能有变
u_c = input(">>>") #1 #等待用户输入选择输入对应的大类如输入1代表选择了动物2代表选择了植物,这里为了方便假设输入为数字
u_c = int(u_c) #将输入的字符串转换成整数
li1 = [] #定义一个空列表用来存放用户选择大类下的小分类
whilego:for i,v in enumerate(dic[li[u_c-1]],1): #for循环根据用户输入假如输入1选择动物则迭代动物这个key下面的分类
print(i,v) #列出对应的序号及分类
li1.append(v) #假如选择的是动物li1 = ["禽类","哺乳类动物","两栖动物"]
u_c1 = input(">>>>") #提示用户输入对应的数字
u_c1 =int(u_c1)whilego:for i in dic[li[u_c-1]][li1[u_c1-1]]: #根据用户输入列出对应的三级分类项,假如选择了禽类对应的数字列出所有禽类
print(i)
u_c2= str(input(">>>>>"))
u_c2=u_c2.lower()if u_c2 == "b":
li1.clear()break
elif u_c2 == "q":
go=Falsebreak
最后可以输入b清空li1列表然后返回上一层,输入q把go至为False然后退出整个循环
继续优化在第二层也能输入b返回上一层输入q退出
vim day13-4.py
dic ={"植物":
{"草本植物":
["牵牛花","瓜叶菊","翠菊","冬小麦","甜菜"],"水本植物":
["乔木","灌木","半灌木","如松","杉","樟"],"水生植物":
["荷花","千屈菜","菖蒲","黄菖蒲","水葱","再力花","梭鱼草"]},"动物":
{"两栖动物":
["山龟","山鳖","石蛙","娃娃鱼","蟾蜍","龟","鳄鱼","蜥蜴","蛇"],"禽类":
["雏鸡","原鸡","长命鸡","昌国鸡","斗鸡","长尾鸡","乌骨鸡"],"哺乳类动物":
["虎","狼","鼠","鹿","貂","猴","嫫","树懒","斑马","狗"]}}#循环加序号打印key
li = [] #定义一个空列表,用来存放第一次循环的key及本题的"动物","植物"
go = True #定义无限循环参数go初始值为True当程序运行过程中输入q则变成False退出整个循环
whilego:for i,v in enumerate(dic,1): #for循环,使用enumerate方法生成序号,序号是从1开始
print(i,v) #打印序号加第一个key 分别为1 动物 2 植物 由于字典是无序的这里序号可能会有变化
li.append(v) #将key作为列表的元素追加至列表li此时 li = ["动物","植物"]同样顺序可能有变
u_c = input("请输入一级分类") #等待用户输入选择输入对应的大类如输入1代表选择了动物2代表选择了植物
u_c = int(u_c) #将输入的字符串转换成整数
li1 = [] #定义一个空列表用来存放用户选择大类下的小分类
whilego:for i,v in enumerate(dic[li[u_c-1]],1): #for循环根据用户输入假如输入1选择动物则迭代动物这个key下面的分类
print(i,v) #列出对应的序号及分类
li1.append(v) #假如选择的是动物li1 = ["禽类","哺乳类动物","两栖动物"]
u_c1 = input("请输入二级分类输入b返回上一层输入q退出") #提示用户输入对应的分类数字或者输入b返回输入q退出
if u_c1.isdigit(): #判断u_c1输入的是否是数字
u_c1 =int(u_c1)whilego:for i in dic[li[u_c-1]][li1[u_c1-1]]: #根据用户输入列出对应的三级分类项,假如选择了禽类对应的数字列出所有禽类
print(i)
u_c2= str(input("输入b返回上一层输入q退出"))
u_c2=u_c2.lower()if u_c2 == "b":
li1.clear()break
elif u_c2 == "q":
go=Falsebreak
elif u_c1.isalpha(): #u_c1如果不是数字判断是否是字母
u_c1 = u_c1.lower() #全部转换成小写
if u_c1 == "b":
li.clear()#如果输入是b清空li退出循环
break
elif u_c1 == "q": #如果输入q则退出整个循环
go =Falsebreak
这样在任何位置都可以输入b返回上一层 输入q退出了
以上可以实现在二级分类及列出动植物名称的时候退出或者返回但是一级分类哪里没有判断输入是否为数字,下面加一个if判断是否为数字并且小于或者等于一级分类的数,如果输入q则退出
vim day13-5.py
dic ={"植物":
{"草本植物":
["牵牛花","瓜叶菊","翠菊","冬小麦","甜菜"],"水本植物":
["乔木","灌木","半灌木","如松","杉","樟"],"水生植物":
["荷花","千屈菜","菖蒲","黄菖蒲","水葱","再力花","梭鱼草"]},"动物":
{"两栖动物":
["山龟","山鳖","石蛙","娃娃鱼","蟾蜍","龟","鳄鱼","蜥蜴","蛇"],"禽类":
["雏鸡","原鸡","长命鸡","昌国鸡","斗鸡","长尾鸡","乌骨鸡"],"哺乳类动物":
["虎","狼","鼠","鹿","貂","猴","嫫","树懒","斑马","狗"]}}#循环加序号打印key
li = [] #定义一个空列表,用来存放第一次循环的key及本题的"动物","植物"
go = True #定义无限循环参数go初始值为True当程序运行过程中输入q则变成False退出整个循环
whilego:for i,v in enumerate(dic,1): #for循环,使用enumerate方法生成序号,序号是从1开始
print(i,v) #打印序号加第一个key 分别为1 动物 2 植物 由于字典是无序的这里序号可能会有变化
li.append(v) #将key作为列表的元素追加至列表li此时 li = ["动物","植物"]同样顺序可能有变
u_c = input("请输入一级分类输入q退出") #等待用户输入选择输入对应的大类如输入1代表选择了动物2代表选择了植物
if u_c.isdigit() and int(u_c) <= i: #判断用户输入一级分类是否为数字并且是否小于一级分类数
u_c = int(u_c) #将输入的字符串转换成整数
li1 = [] #定义一个空列表用来存放用户选择大类下的小分类
whilego:for i,v in enumerate(dic[li[u_c-1]],1): #for循环根据用户输入假如输入1选择动物则迭代动物这个key下面的分类
print(i,v) #列出对应的序号及分类
li1.append(v) #假如选择的是动物li1 = ["禽类","哺乳类动物","两栖动物"]
u_c1 = input("请输入二级分类输入b返回上一层输入q退出") #提示用户输入对应的分类数字或者输入b返回输入q退出
if u_c1.isdigit(): #判断u_c1输入的是否是数字
u_c1 =int(u_c1)whilego:for i in dic[li[u_c-1]][li1[u_c1-1]]: #根据用户输入列出对应的三级分类项,假如选择了禽类对应的数字列出所有禽类
print(i)
u_c2= str(input("输入b返回上一层输入q退出"))
u_c2=u_c2.lower()if u_c2 == "b":
li1.clear()break
elif u_c2 == "q":
go=Falsebreak
elif u_c1.isalpha(): #u_c1如果不是数字判断是否是字母
u_c1 = u_c1.lower() #全部转换成小写
if u_c1 == "b":
li.clear()#如果输入是b清空li退出循环
break
elif u_c1 == "q": #如果输入q则退出整个循环
go =Falsebreak
elif u_c.isalpha(): #用户输入的一级分类如果是字母并且为q则退出
u_c ==u_c.lower()if u_c == "q":
go=Falsebreak
流程图如下