新手:python里面while循环1——else

Python 中,无论是 while 循环还是 for 循环,其后都可以紧跟着一个else 代码块,它的作用是当循环条件为 False 跳出循环时,程序会最先执行 else 代码块中的代码。
下面是一个三层菜单的案例,用两个标志位实现返回、退出,else很好用,下一章不用函数进行代码优化。

点击查看代码
# -*- coding:utf-8 -*-
# __author: Andy Liu
# Date: 2023/3/1

menu = {
    "lucky_shop": {
        "fruit": {
            "banana": {"banana": "8yuan"},
            "apple": {"apple": "7yuan"},
            "tangerine": {
                "Sugar Tachibana": "5yuan",
                "Citrus": "8yuan"

            }
        },
        "stationery": {
            "pencil": {"pencil": "2yuan"},
            "pen": {"pen": "5yuan"},
            "notebook": {
                "small": "1yuan",
                "middle": "2yuan",
                "big": "3yuan"
            }
        },
        "kitchen": {
            "knife": {"knife": "10yuna"},
            "wok": {
                "middle": "20yuan",
                "big": "25yuan"
            },
            "spatula": {"spatula": "13yuan"}
        }
    },
    "seven_eleven": {None},
    "super_market": {None}
}

back_flag = False
quit_flag = False

while not quit_flag and not back_flag:
    print("[press 'b' return to the previous layer.]")
    print("[press 'q' to exit.]")
    while not back_flag and not quit_flag:
        print("-----------")
        for key in menu:
            print(key)
        choice = input("1.>> ").strip()
        if choice == "q":
            quit_flag = True
        elif choice == "b":
            back_flag = True
        elif choice in menu:
            while not back_flag and not quit_flag:
                print("-----------")
                for key1 in menu[choice]:
                    print(key1)
                choice1 = input("2.>> ").strip()
                if choice1 == "q":
                    quit_flag = True
                elif choice1 == "b":
                    back_flag = True
                elif choice1 in menu[choice]:
                    while not back_flag and not quit_flag:
                        print("-----------")
                        for key2 in menu[choice][choice1]:
                            print(key2)
                        choice2 = input("3.>> ").strip()
                        if choice2 == "q":
                            quit_flag = True
                        elif choice2 == "b":
                            back_flag = True
                        elif choice2 in menu[choice][choice1]:
                            while not back_flag and not quit_flag:
                                print("-----------")
                                for key3 in menu[choice][choice1][choice2]:
                                    print(f"{key3} need {menu[choice][choice1][choice2][key3]}")
                                choice3 = input("4.>> ").strip()
                                if choice3 == "q":
                                    quit_flag = True
                                elif choice3 == "b":
                                    back_flag = True
                                elif choice3 in menu[choice][choice1][choice3]:
                                    while not back_flag and not quit_flag:
                                        print("-----------")
                                        if choice3 in menu[choice][choice1][choice2][choice3]:
                                            for key4 in menu[choice][choice1][choice2][choice3]:
                                                print(f"{key4} need {menu[choice][choice1][choice2][choice3][key4]}")
                                        else:
                                            print("no element!")
                                            choice4 = input("5.>> ").strip()
                                            if choice4 == "q":
                                                quit_flag = True
                                            elif choice4 == "b":
                                                back_flag = True
                                    else:
                                        back_flag = False
                                else:
                                    print("invalid input")
                            else:
                                back_flag = False
                        else:
                            print("invalid input")
                    else:
                        back_flag = False
                else:
                    print("invalid input")
            else:
                back_flag = False
        else:
            print("invalid input")
    else:
        back_flag = False

print("Welcome again!")


posted @ 2023-03-02 11:39  羊羊羊01  阅读(113)  评论(0)    收藏  举报