Python week1-练习2三级目录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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三级目录的主要内容,如果未能解决你的问题,请参考以下文章

Python week1-练习1登陆接口

Python练习-三级菜单与"片儿"无关!

python 练习2字典打印三级菜单

python练习——moudule01——三级菜单

Coursera课程 R-Programming Week1 编程练习代码

简单练习:Python三级菜单优化