首页 > 代码库 > Python week1-练习2三级目录

Python week1-练习2三级目录

文件:practise2.tar

练习:多级菜单

  1. 三级菜单
  2. 依次选择菜单
  3. 列表和数据字典
#!/usr/bin/env python
#Author:Austin
def match_name(level):
    _file = open("dictionary.txt","r")
    line = _file.readline()
    while line:
        _level = line.split(":")[0]
        _name = line.split(":")[1][:-1]
        if _level == level:
            name = _name
            break
        line = _file.readline()
    _file.close()
    return name

def level1_print():
    print("------List------")
    _file = open("list.txt","r")
    line = _file.readline()
    _tmp_level = ""
    while line:
        level1 = line.split(":")[0]
        if level1 != _tmp_level:
            name = match_name(level1)
            print(level1,name)
        _tmp_level = level1
        line = _file.readline()
    print("----------------")
    chose = input("Plese chose one:(1/2/3)")
    _file.close()
    return chose

def level2_print(level):
    print("-----{level}-----".format(level=level))
    _file = open("list.txt","r")
    line = _file.readline()
    _tmp_level = ""
    while line:
        level1 = line.split(":")[0]
        level2 = line.split(":")[1]
        if level1 == level and level2 != _tmp_level:
            name = match_name(level2)
            print(level2,name)
        _tmp_level = level2
        line = _file.readline()
    _file.close()
    print("----------")
    chose = input("Back or chose one:")
    return chose

def level3_print(level):
    print("-----{level}-----".format(level=level))
    _file = open("list.txt","r")
    line = _file.readline()
    _tmp_level = ""
    while line:
        level2 = line.split(":")[1]
        level3 = line.split(":")[2][:-1]
        if level2 == level and level3 != _tmp_level:
            name = match_name(level3)
            print(name)
        _tmp_level = level3
        line = _file.readline()
    _file.close()
    print("----------")
    chose = input("Back or chose one:")
    return chose

current_level = 1
last_chose = ""
current_chose = ""
chose = ""

while True:
    if current_level == 1:
        chose = level1_print()
        current_level = 2
    elif current_level == 2 and chose != "b":
        last_chose = chose
        chose = level2_print(chose)
        current_level = 3
    elif current_level == 3 and chose != "b":
        chose = level3_print(chose)
    elif current_level == 3 and chose == "b":
        chose = level2_print(last_chose)
        current_level = 2
    elif current_level == 2 and chose == "b":
        chose = level1_print()

后记:

1.山炮程序员又没写注释

2.有bug,实在写累了不想改了。有些地方自己也觉得很烂。

3.等看到Alex怎么写的,来更新。

Python week1-练习2三级目录