Python避免递归

时间:2017-06-27 10:58:55

标签: python recursion

我有一个测验程序,它有一个主菜单,可以指向学生或教师登录。从登录后,他们会被导向学生菜单或教师菜单。

我的导师说这是递归式的,我必须从主菜单进入密码检查,返回主菜单,返回True值,然后进入教师/学生菜单。

如何获得返回的值,以及如何将其编码回主菜单?可悲的是,我被要求做的远远超过了我们在课程中所教授的内容,因此我需要

目前菜单代码为:

 def Main_Menu():
    ##First Menu will splinter off to the Teacher/Student System
        print ("Welcome to the Computer Science Keyword Quiz")
        print ("*" * 30)
        print ("Let's get you to the right place.  Here are your options today:")
        print ("Enter 1 for the Teacher Menu")
        print ("Enter 2 for the Student Menu")
        print ("Enter 3 to Quit")
        while True:
            try:
                choice = int(input ("Enter choice: "))
                if choice == 1:
                    staff_password_check()
                    break
                elif choice == 2:
                    student_user_check()
                    break
                elif choice == 3:
                    break
                else:
                    print ("Invalid choice. Only enter 1 or 2 or 3")

            except ValueError:
                print ("Invalid choice.  Please enter 1 or 2 or 3")

        exit

并且,作为示例,登录检查是:

def staff_password_check():
    password = "Pa55Word"
##password coded in by owner who will change regularly and inform staff
    attempts = 0
##set so user has limited chances to access the system
    print("Welcome to the Teacher Menu")
    print('Time to check your password')
    while attempts <4 :     
        entered_password = input("Please enter your password: ")  

        attempts = attempts + 1
        if entered_password != password:
           print('Incorrect password entered')
##error printed if user has input incorrect password     

        if entered_password == password:
           print("Welcome Teacher")
           print ("What do you want to do today?")
           Teacher_Menu()
## if correct password is entered the user is taken to the Teacher Menu                      


    if entered_password != password:
       print("You have forgotten your password.  See Mrs Jones")
       anykey = input ("Hit any key to return to the Main Menu: ")
       Main_Menu()

2 个答案:

答案 0 :(得分:0)

尝试在Main_Menu函数

中实现这样的功能
    if choice == 1:
         if(staff_password_check()):
             print("Welcome Teacher")
             print ("What do you want to do today?")
             Teacher_Menu()
             break
         else:
             print("You have forgotten your password.  See Mrs Jones")
             anykey = input ("Hit any key to return to the Main Menu: ")
             Main_Menu()

并为密码不匹配的案例实施else条件。

另外,修改staff_password_check()功能的while条件。

   if entered_password == password:
       return True

   if entered_password != password:
       return False

希望这会有所帮助。

答案 1 :(得分:0)

您应该传递Main_Menu参数中的所有变量

def Main_Menu(password_success = None, choice = None):
    if password_success:
        if choice == 1:
            Teacher_Menu()
        else:
            Student_Menu()
    else:
        print ("Welcome to the Computer Science Keyword Quiz")
        print ("*" * 30)
        print ("Let's get you to the right place.  Here are your options today:")
        print ("Enter 1 for the Teacher Menu")
        print ("Enter 2 for the Student Menu")
        print ("Enter 3 to Quit")
        while True:
            try:
                choice = int(input ("Enter choice: "))
                if choice == 1:
                    staff_password_check()
                    break
                elif choice == 2:
                    student_user_check()
                    break
                elif choice == 3:
                    break
                else:
                    print ("Invalid choice. Only enter 1 or 2 or 3")

            except ValueError:
                print ("Invalid choice.  Please enter 1 or 2 or 3")
        exit()

def staff_password_check():
    password = "Pa55Word"
    attempts = 0
    print("Welcome to the Teacher Menu")
    print('Time to check your password')
    while attempts <4 :     
        entered_password = input("Please enter your password: ")  

        attempts = attempts + 1
        if entered_password != password:
            print('Incorrect password entered')  

        if entered_password == password:
            Main_Menu(True, 1)                    


     if entered_password != password:
        print("You have forgotten your password.  See Mrs Jones")
        Main_Menu(False, None)