Python week1-練習2三級目錄

文件:practise2.tarpython

練習:多級菜單程序員

  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()

後記:spa

1.山炮程序員又沒寫註釋code

2.有bug,實在寫累了不想改了。有些地方本身也以爲很爛。orm

3.等看到Alex怎麼寫的,來更新。blog

相關文章
相關標籤/搜索