python3 練習題 多級選單

2022-09-07 02:33:15 字數 2566 閱讀 3004

'''

多級選單

需求:1.現有省/市/縣3級結構,要求程式啟動後,允許使用者可依次選擇進入各子選單

2.可在任意一級選單返回上一級

3.可以在任意一級選單退出程式

所需新知識點: 列表/字典

'''menu =,

"商河": {},

"萊蕪": {}

},"濟寧

": ,

"微山": {},

"嘉祥": {}

},"曲阜

": ,

"陵城": {},

"防山": {}}},

"北京": ,

"永定路

": {},

"中關村

": {}

},"昌平

": ,

"南口": {},

"沙河": {}

},"朝陽

": ,

"三里屯

": {},

"雙井": {}}},

"上海": ,

"外灘": {},

"瑞金二路

": {}

},"徐匯

": ,

"天平路

": {},

"楓林路

": {}

},"長寧

": ,

"新華路

": {},

"江蘇路

": {}}},

}current_menu =menu

layers =

while

true:

for i in

current_menu:

print

(i) choice = input("

>:

").strip()

if choice in

current_menu:

current_menu =current_menu[choice]

elif choice.upper() == "b"

:

if len(layers) ==0:

print("

已經到頂了")

continue

current_menu =layers.pop()

----------------------------------------------微調1------------------------------------current_menu =menu

choices =

while

true:

for i in

current_menu:

print

(i) choice = input("

>:

").strip()

if choice in

current_menu:

current_menu =current_menu[choice]

elif choice.upper() == "b"

: current_menu =menu

if len(choices) ==0:

print("

已經到頂了")

continue

choices.pop()

for i in

choices:

current_menu =current_menu[i]

''''''

------------------------------------------微調2---------------------------------------current_menu =menu

choices =

while

true:

for i in

current_menu:

print

(i)

print("

q退出 b返回上一級")

if len(choices) >= 1:

choice = input("

%s>:

" % '

>

'.join(choices)).strip()

else

: choice = input("

>:

").strip()

if choice in

current_menu:

current_menu =current_menu[choice]

elif choice.upper() == "b"

: current_menu =menu

if len(choices) ==0:

print("

已經到頂了")

continue

choices.pop()

for j in

choices:

current_menu =current_menu[j]

elif choice.upper() == "b"

:

break

Python3道基礎練習題

練習 輸入乙個整數n,判斷這個整數是否是素數 prime 素數是指只能被1 和自身整除的數 如 2 3 5 7 11.方法 用排除法.一但n能被2 n 1的數整除就不是素 數,否則就一定是素數 n int input 請輸入乙個整數 if n 2 print n,不是素數 exit 方法 1 fla...

Python3練習題系列(04)

製作乙個遊戲 函式 if elif else,while,exit bastard 英 b st d b st 美 b st d adj.私生的 n.私生子 greedy 英 gri d 美 gridi adj.貪婪的 貪吃的 渴望的 defbear room print there is a be...

python3 練習題100例 (三)

題目三 乙個整數,它加上100後是乙個完全平方數,再加上168又是乙個完全平方數,請問該數是多少?usr bin env python3 coding utf 8 題目三 乙個整數,它加上100後是乙個完全平方數,再加上168又是乙個完全平方數,請問該數是多少?author fan lijun im...