展示三级菜单

     本程序在suse11sp3, python2.6.8 中测试通过。 用address 字典记录省市区三级菜单。address_v2.py 是在 python2.6 下的脚本,  address.py 是在 python3.0 以上的脚本,把 address_v2.py 中的 raw_input 换成了 input。

   程序如下:

#/usr/bin/env python #-*- coding: utf-8 -*- #--author: zhoudingzhao #用字典记录三级菜单:  {  "省" : { "市" : [ "区"] } , "省" : { "市" : [ "区"] }  } address = {  "jiangsu":   { "nanjing":["jiangning", "pukou","qinhuai"] , "suzhou":["canglang","pingjiang"] } ,         "shanghai":  { "xuhui": ["tianlin","caohejing"] , "pudong":["zhangjiang"] } ,         "beijing" :  { "dongcheng":["hepingli","dongzhimen","jianguomen","dongsijie"] , "chaoyang":["tuanjiehu","liulitun","yayuncun","panjiayuan","wangjing"] , "haidian":["zhongguancun","qinghuayuan","wenquanzhen"] ,"changping":["shahe","huilongguan","xiaotangshan","machikou"] } }

flag1 = True                                                 #进入第一层循环,提示输入省。 while flag1:     print(address.keys())     province = raw_input("input the province:")     if province in address.keys():                          #如果输入省正确,进入第二层循环,提示输入市。         print(address[province].keys())

        flag2 = True         while flag2:             city = raw_input("input the city:")             if city in address[province].keys():            #如果输入市正确,进入第三层循环,提示输入区。                 print(address[province][city])                 area = raw_input("input the area:")                 flag3 = True                 while flag3:                     if area in address[province][city]:     #输入了正确的省市区, 打印省市区信息,结束。                         print ("Your address is %s,%s,%s" %(province , city, area) )                         flag1 = False                         flag2 = False                         flag3 = False                         break                     elif(area == 'b'):                     #输入区的地方,输入了 b, 跳出第三层循环,进入第二层循环,重新输入城市。                         print(address[province].keys())                         flag3 = False

                    else:                         print("input area wrong")         #输入了错误的区,包括输入 q , 提示输入区错误,结束。                         flag1 = False                         flag2 = False                         flag3 = False                         break             elif(city == 'b'):                           #输入城市的时候,输入了 b,跳出第二层循环,进入第一层循环,重新输入省份。                 #print(address.keys())                 flag2 = False             else:                                        #输入了错误的市,包括输入 q , 提示输入市错误,结束。                 print("input city wrong")                 flag1 = False                 flag2 = False                 break

    elif( province == 'b'):                               #输入省的地方,输入了 b, 返回到输入省的地方重新循环。         #flag1 = False         continue     else:                                                 #输入了错误的省,(或者是 q), 打印输出错误,结束。         flag1 = False         print("--input province wrong!")         break

 

你可能感兴趣的:(展示三级菜单)