当if语句为真时,为什么我的脚本会继续'while'循环?

时间:2017-08-20 00:15:19

标签: python-3.x

  

在第11行,用户在EasyGui输入框中选择取消按钮,为什么代码会继续“while”循环,即使用户的选择返回'NoneType?它不符合'if'陈述的条件吗?脚本不应该返回到被调用的'main'函数吗?

这是完整的脚本。

from easygui import*

def  children_menu():
    title2 = "Children."
    children_menu = ["1: Register a child.", "2: List of registered children."]
    button = buttonbox("Choose an option", title2, choices=children_menu)

    if button == children_menu[0]:
        enter1 = enterbox(msg="Enter the child's first name.", title=' ', default='', strip=True, image=None, root=None)
        if enter1 is None:
            main()
            return
        else:
            while enter1 == "" or enter1.isalpha() == False:
                enter1 = enterbox(msg="Invalid entry. Please enter the child's first name using letters only.",title=' ', default='', strip=True, image=None, root=None)

        enter2 = enterbox(msg="Enter the child's last name.", title=' ', default='', strip=True, image=None, root=None)  # ask user to enter child's first name
        while enter2 == "" or enter2.isalpha() == False:
            if enter2 is None:
                main()
            else:
                enter2 = enterbox(msg="Invalid entry. Please enter the child's last name using letters only.", title=' ', default='', strip=True, image=None,root=None)

                child_name = (enter1 + " " + enter2).title()
                file = open("Children.txt", 'a')
                file.write(child_name + "\n")
                file.close()

    if button == children_menu[1]:
        file = open("Children.txt", 'r')
        lineList = file.readlines()
        lineList.sort()  
        print("List of children's names in alphabetical order:")
        for line in lineList:
            print(line, end="")
        file.close()
        main()

def main():
    title1 = "Main menu."
    main_menu = ["1: Children", "2: Groups", "3: Educators", "4: Tags", "5: Cancel"]
    button = buttonbox("Choose an option", title1, choices=main_menu)

    if button == main_menu[0]:
        children_menu()


    elif button == main_menu[1]:
        group_menu()

    elif button == main_menu[2]:
        button = "3"
        title4 = "Educators."
        educator_menu = ["1: Register an educator.", "2: List of registered educators."]
        button = buttonbox("Choose an option", title4, choices=educator_menu)

    elif button == main_menu[3]:
        button = "4"
        title4 = "Tags."
        tags_menu = ["1: Create tags", "2: List of tags."]
        button = buttonbox("Choose an option", title4, choices=tags_menu)

    else:
        button = "You cancelled."

main()

0 个答案:

没有答案