利用字典編寫菜單程式

來源:互聯網
上載者:User

標籤:平治   first   second   退出   我愛我家   inpu   進入   pre   愛我   

要求:

  1. 共有三級菜單可選擇
  2. 在每層菜單可進入下一菜單、返回上一層菜單和退出

代碼:

 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:James Tao 4  5 data={ 6     ‘北京‘:{ 7         ‘昌平‘:{ 8             ‘沙河‘:[‘oldboy‘,‘test‘], 9             ‘天通苑‘:[‘鏈家地產‘,‘我愛我家‘]10         },11         ‘朝陽‘:{12             ‘望京‘:[‘平治‘,‘陌陌‘],13             ‘國貿‘:{‘CCIC‘,‘HP‘},14             ‘東直門‘:{‘Advent‘,‘飛信‘}15         },16         ‘海澱‘:{}17     },18     ‘山東‘:{19         ‘德州‘:{},20         ‘青島‘:{},21         ‘濟南‘:{}22     },23     ‘廣東‘:{24         ‘東莞‘:{},25         ‘常熟‘:{},26         ‘佛山‘:{}27     }28 }29 30 flag=True31 32 while flag:33 34     for i in data:35         print(i)36     first_choice=input(‘選擇進入下一級菜單/輸入Q退出:‘)37 38     #進入第一級菜單39     if first_choice in data:40 41         while flag:42 43             for j in data[first_choice]:44                 print(‘\t‘,j)45             second_choice=input(‘選擇進入下一級菜單/輸入R返回上一級/輸入Q退出:‘)46 47             #進入第二級菜單48             if second_choice in data[first_choice]:49                 while flag:50 51                     for z in data[first_choice][second_choice]:52                         print(‘\t\t‘, z)53                     third_choice=input(‘選擇進入下一級菜單/輸入R返回上一級/輸入Q退出:‘)54 55                     #進入第三級菜單56                     if third_choice in data[first_choice][second_choice]:57                         while flag:58 59                             for k in data[first_choice][second_choice][third_choice]:60                                 print(‘\t\t\t‘, k)61                             fourth_choice=input(‘最後一層,輸入R返回上一級/輸入Q退出:‘)62 63                             #是否返回到第三級或退出64                             if fourth_choice==‘R‘:65                                 break66                             elif fourth_choice==‘Q‘:67                                 flag=False68 69                     #是否返回到第二級或退出70                     if third_choice==‘R‘:71                         break72                     elif third_choice == ‘Q‘:73                         flag = False74 75             #是否返回到第一級或退出76             if second_choice==‘R‘:77                 break78             elif third_choice == ‘Q‘:79                 flag = False80 81     #是否退出82     if first_choice==‘Q‘:83         break

 

利用字典編寫菜單程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.