1. 程式人生 > >Python之雙色球選購和三級菜單問題

Python之雙色球選購和三級菜單問題

設置 狀態 oos googl side key值 google n) 網易

1:雙色球選購
# 1 雙色球(假設一共八個球,6個紅球,球號1-32、2個藍球,球號1-16)
# 2 確保用戶不能重復選擇,不能超出範圍
# 3 用戶輸入有誤時有相應的錯誤提示
# 4 最後展示用戶選擇的雙色球的號碼
select_red_ball = []
while True:
n = int(input(‘請輸入你要選擇的紅色球(1-32):‘))
if 0 < n < 33:
if n not in select_red_ball:
select_red_ball.append(n)
else:
print(‘number %d is already exist in red ball list‘ % n)

else:
print(‘only can select n between 1-32‘)
if len(select_red_ball) == 6:
break
select_red_ball.sort()
select_blue_ball = []
while True:
n = int(input(‘請輸入你要選擇的藍色球(1-32):‘))
if 0 < n < 17:
if n not in select_blue_ball:
select_blue_ball.append(n)
else:

print(‘number %d is already exist in blue ball list‘ % n)
else:
print(‘only can select n between 1-16‘)
if len(select_blue_ball) == 2:
break
select_blue_ball.sort()
print(‘red ball %d‘ % select_red_ball)
print(‘blue ball %d‘ % select_blue_ball)

2 :三級菜單

  • 數據結構:
    menu = {
        ‘北京‘:{
            ‘海澱‘:{
                ‘五道口‘:{
                    ‘soho‘:{},
                    ‘網易‘:{},
                    ‘google‘:{}
                },
                ‘中關村‘:{
                    ‘愛奇藝‘:{},
                    ‘汽車之家‘:{},
                    ‘youku‘:{},
                },
                ‘上地‘:{
                    ‘百度‘:{},
                },
            },
            ‘昌平‘:{
                ‘沙河‘:{
                    ‘老男孩‘:{},
                    ‘北航‘:{},
                },
                ‘天通苑‘:{},
                ‘回龍觀‘:{},
            },
            ‘朝陽‘:{},
            ‘東城‘:{},
        },
        ‘上海‘:{
            ‘閔行‘:{
                "人民廣場":{
                    ‘炸雞店‘:{}
                }
            },
            ‘閘北‘:{
                ‘火車站‘:{
                    ‘攜程‘:{}
                }
            },
            ‘浦東‘:{},
        },
        ‘山東‘:{},
    }
    
    需求:
    可依次選擇進入各子菜單
    可從任意一層往回退到上一層
    可從任意一層退出程序
    所需新知識點:列表、字典

代碼一:

exit_flag = False
while not exit_flag:
for i in menu:
print(i)
choice1 = input("選擇進入1>>:")
if choice1 in menu:
while not exit_flag:
for i2 in menu[choice1]:
print("\t", i2)
choice2 = input("選擇進入2>>:")
if choice2 in menu[choice1]:
while not exit_flag:
for i3 in menu[choice1][choice2]:
print("\t\t", i3)
choice3 = input("選擇進入3>>:")
if choice3 in menu[choice1][choice2]:
for i4 in menu[choice1][choice2][choice3]:
print("\t\t", i4)
choice4 = input("最後一層,按b返回>>:")
if choice4 == "b":
pass
elif choice4 == "q":
exit_flag = True
if choice3 == "b":
break
elif choice3 == "q":
exit_flag = True
if choice2 == "b":
break
elif choice2 == "q":
exit_flag = True

代碼二:(優化版本,代碼縮減為15行)

current_layer = menu
parent_layer = [] # 將父級key值放入到列表中
flags = False # 設置標誌位
while not flags:
for key in current_layer:
print(key)
choose = input("請選擇,輸入b返回上一級菜單,輸入q退出菜單:").strip() # 去除空格
if choose in current_layer:
parent_layer.append(current_layer) # 將當前的狀態放入列表中
current_layer = current_layer[choose]
elif choose == ‘b‘:
if parent_layer:
current_layer = parent_layer.pop()
elif choose == ‘q‘:
flags = True
else:
print(‘輸入有誤,請重新輸入‘)

Python之雙色球選購和三級菜單問題