1. 程式人生 > >python系統學習:第三週之簡單的三級選單

python系統學習:第三週之簡單的三級選單

# 三級目錄
info = {
# 一級
'ShanXi': {
# 二級
'JieXiu': {
# 三級
'XiaoSongQv': ['Burn here!'],
'SanSchool': ['Study here']
}
},
'BeiJing': {
'ChaoYang': {
'XianNinghou': ['Live here!']
},
'BeiYuan': {
'TieJian': ['Work Here!']

}
}
}
# 三級目錄的跳轉
# 定義標誌
flag = False
while not flag:
for name1 in info:
print(name1)
# 選擇1
choice = input('>>where?')
# 判斷是否存在
if choice in info:
# 判斷成功列印第二層
while not flag:
for name2 in info[choice]:
print(name2)
# 選擇

choice2 = input('>>where?')
# 判斷是否存在
if choice2 in info[choice]:
# 判斷成功列印第三層
while not flag:
for name3 in info[choice][choice2]:
print(name3)
choice3 = input('>>where?')

if choice3 in info[choice][choice2]:
for name4 in info[choice][choice2][choice3]:
print(name4)
choice4 = input('最後一層,b返回,q退出!')
if choice4 == 'b':
pass # 佔位符 我什麼都不做
elif choice4 == 'q':
flag = True
elif choice3 == 'b':
break
elif choice3 == 'q':
flag = True
elif choice2 == 'b':
break
elif choice2 == 'q':
flag = True
# 輸入B返回
elif choice == 'b':
pass
# 輸入q退出
elif choice == 'q':
flag = True

  或許你認為這個寫法比較弱智,但是學習程式碼都是從麻煩到簡單,學會了最原始的寫法,能更好的瞭解執行機制,等後面學了函式等,就變得簡單了。