Python dictionary provides a simple three-level menu,

Source: Internet
Author: User

Python dictionary provides a simple three-level menu,

Data = {
"Beijing ":{
"Changping": {"shahe": ["oldboy", "test"], "tiantongyuan": ["linked property", "I love my family"]},
"Chaoyang": {"Wangjing": ["Mercedes-Benz", "momo"], "International trade": ["CICC", "HP"], "Dongzhimen ": ["advent", "fetion"]},
"Haidian ":{}
},
"Shandong ":{
"Texas ":{},
"Qingdao ":{},
"Jinan ":{}
},
"Guangdong ":{
"Dongguan ":{},
"Changshu ":{},
"Foshan ":{}
}
}
Exit_flag = False
While not exit_flag:
For I in data:
Print (I)
Choice_s = input ("enter the name of any of the preceding provinces or municipalities >> :")
If choice_s in data:
While not exit_flag:
For I in data [choice_s]:
Print ("\ t", I)
Choice_q = input ("enter the name of any of the above cities >> :")
If choice_q in data [choice_s]:
While not exit_flag:
For I in data [choice_s] [choice_q]:
Print ("\ t", I)
Choice_j = input ("enter the name of any of the preceding streets >> :")
If choice_j in data [choice_s] [choice_q]:
For I in data [choice_s] [choice_q] [choice_j]:
Print ("\ t", I)
Last = input ("last layer, return by B >>> :")
If last = "B ":
Pass
Elif last = "q ":
Exit_flag = True
If choice_j = "B ":
Break
Elif choice_j = "q ":
Exit_flag = True
If choice_q = "B ":
Break
Elif choice_q = "q ":
Exit_flag = True


Key points:
1. hierarchical relationship of dictionaries
2. break terminates this loop, and pass does nothing. It is a placeholder.
3. cleverly use exit_flag = False

While not exit_flag:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.