Python-三级菜单

要求:
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])

格式自行调整!
加入代码后:
这里写图片描述
加入代码前:
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值