簡體   English   中英

Python避免遞歸

[英]Python avoiding recursion

我有一個測驗程序,該程序有一個主菜單,可直接定向到學生或教師的登錄。然后從該登錄中將其定向到學生菜單或教師菜單。

我的老師說這是遞歸的,我必須從“主菜單”轉到密碼檢查,然后以“真”值返回“主菜單”,然后再進入“教師/學生”菜單。

如何獲取返回的值,該如何執行呢?如何將其編碼回主菜單? 可悲的是,我被要求做的是超出我們課程所教的內容,因此我需要

目前,菜單代碼為:

 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()

嘗試在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

希望這會有所幫助。

您應該在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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM