要求:
1、根据用户选择的数字进入指定的菜单
2、如果输入的数字不属于当前选项,停留在当前菜单
3、”q“为退出,”b”为返回上一级菜单
# 三级菜单--if-else版本
import sys
tupf = ("河北省", "湖南省", "河南省")
tups=[["石家庄", "秦皇岛", "衡水"],["长沙", "长勺", "长宇"],["郑州", "开封", "洛阳"]]
tupt=[[["桃城区", "桃花怒", "长安区"],["开发区", "海港区", "昌黎"],["饶阳", "安平", "深州"]],
[["长沙-1", "长沙-2", "长沙-3"], ["长勺-1", "长勺-2", "长勺-3"], ["长宇-1", "长宇-2", "长宇-3"]],
[["郑州-1", "郑州-2", "郑州-3"], ["开封-1", "开封-2", "开封-3"], ["洛阳-1", "洛阳-2", "洛阳-3"]]]
tupp=[[["桃城区-h", "桃花怒-h", "长安区-h"],["开发区-h", "海港区-h", "昌黎-h"],["饶阳-h", "安平-h", "深州-h"]],
[["长沙-1-h", "长沙-2-h", "长沙-3-h"], ["长勺-1-h", "长勺-2-h", "长勺-3-h"], ["长宇-1-h", "长宇-2-h", "长宇-3-h"]],
[["郑州-1-h", "郑州-2-h", "郑州-3-h"], ["开封-1-h", "开封-2-h", "开封-3-h"], ["洛阳-1-h", "洛阳-2-h", "洛阳-3-h"]]]
#列出选项菜单
def check(list):
for i in range(len(list)):
print(i+1,list[i])
return input("your choose:")
#三级回滚自己或者回滚上级
def threeMenuselft(tupf, tups, tupt,first,second):
three = check(tupt[int(first) - 1][int(second) - 1])
if three.lower() == "q":
sys.exit()
elif three.lower() == "b": # 回滚石家庄
threeMenuself(tupf, tups, tupt, first)
elif not(three.isdigit())or int(three) > len(tupt):
threeMenuselft(tupf, tups, tupt, first, second)
else:
print(tupp[int(first) - 1][int(second) - 1][int(three) - 1])
#二级回滚自己或者回滚上级
def threeMenuself(tupf, tups, tupt,first):
second = check(tups[int(first)-1])
if second.lower() == "q":
sys.exit()
elif second.lower() == "b":
threeMenu(tupf, tups, tupt)
elif not(second.isdigit())or int(second) > len(tups):
threeMenuself(tupf, tups, tupt, first)
else:
three = check(tupt[int(first)-1][int(second)-1])
if three.lower() == "q":
sys.exit()
elif three.lower() == "b": # 回滚石家庄
threeMenuself(tupf, tups, tupt, first)
elif not(three.isdigit())or int(three) > len(tupt):
threeMenuselft(tupf, tups, tupt, first, second)
else:
print(tupp[int(first) - 1][int(second) - 1][int(three) - 1])
def threeMenu(tupf,tups,tupt):
first=check(tupf)
if first.lower()=="q":
sys.exit()
elif first.lower()=="b":
threeMenu(tupf, tups, tupt)
elif not(first.isdigit())or int(first) > len(tupf):
threeMenu(tupf, tups, tupt)
else:
second=check(tups[int(first)-1])
if second.lower() =="q":
sys.exit()
elif second.lower() == "b":
threeMenu(tupf, tups, tupt)#回滚到第一级
elif not(second.isdigit())or int(second)>len(tupf):
threeMenuself(tupf, tups, tupt, first)#回滚到自己
else:
three=check(tupt[int(first)-1][int(second)-1])
if three.lower() == "q":
sys.exit()
elif three.lower() == "b":#回滚石家庄
threeMenuself(tupf, tups, tupt, first)
elif not(three.isdigit())or int(three) > len(tupt):
threeMenuselft(tupf, tups, tupt,first,second)
else:
print(tupp[int(first)-1][int(second)-1][int(three)-1])
threeMenu(tupf, tups, tupt)
缺点代码冗长,有待完善。
字符数组和包仍然引用上面的,以下为修正版2.0:
# 遍历数组中的元素----选项菜单
def check(list):
for i in range(len(list)):
print(i + 1, list[i])
return input("Choose(q-quit/b-back):")
# 三级菜单-----函数
def threeMenu(tupf, tups, tupt, mindex):
if len(mindex) == 0:
one = check(tupf)
flag = tupf # 设置标志位,为len判断长度做好准备
#此为代码1
elif len(mindex) == 1:
one = check(tups[int(mindex[0]) - 1])
flag = tups
#此为代码2
elif len(mindex) == 2:
one = check(tupt[int(mindex[0]) - 1][int(mindex[1]) - 1])
flag = tupt
#此为代码3
else:
print(tupp[int(mindex[0]) - 1][int(mindex[1]) - 1][int(mindex[2]) - 1])
sys.exit()
if one.lower() == "q":
sys.exit()
elif one.lower() == "b":
mindex.pop() # 将最后一个参数弹出
threeMenu(tupf, tups, tupt, mindex) # 准备返回上级
elif not (one.isdigit()) or int(one) > len(flag):
threeMenu(tupf, tups, tupt, mindex)
else:
mindex.append(one) # 将输入添加到数组中
threeMenu(tupf, tups, tupt, mindex)
# 主函数------------
threeMenu(tupf, tups, tupt, [])
如果显示当前选项菜单同时,也要显示之前已经选择的项目,可以直接将下面三段代码复制粘贴到标号处:
1、
if one.isdigit() and int(one) <= len(flag):#这两行代码仅仅为显示之前
hist.append(tupf[int(one) - 1])#选择的菜单
2、
if one.isdigit() and int(one) <= len(flag):#效果同上
hist.append(tups[int(mindex[0]) - 1][int(one) - 1])
3、
if one.isdigit() and int(one) <= len(flag):#同上
hist.append(tupt[int(mindex[0]) - 1][int(mindex[1]) - 1][int(one) - 1])
格式自行调整!
加入代码后:
加入代码前: