Home > Back-end >  Restarting while loop when returning None from another function
Restarting while loop when returning None from another function

Time:04-15

I am trying to program a Streets and Alleys card game with Python and I'm having some trouble handling when a function returns None. Essentially, my program prompts the menu of choices, then another line calling a function which is get_option. When the get_option is returned, it is returned as a list and the list could be either a single character long (U, R, H, Q), it could be 1 string and 2 ints (MTF 5 2) or just return None. In my main, I'm having trouble trying to restart my original while loop which contains the get_option function being called. If what I'm asking doesn't make sense, let me know and I can try to explain it more in depth. Here is my code:

def get_option():
    '''Prints option menu and takes
    user input to determine which 
    option will follow'''
    while True:
        
        option = str(input("\nInput an option (MTT,MTF,MFT,U,R,H,Q): ")).lower()
        
        option_list = option.split()
        
        mxx = ["MTT", "MTF", "MFT"]
        
        for item in option_list:
            mode = str(item[0])
            for c in mode:
                if c == "u":
                    option_list[0] = c.upper()
                    return option_list
                
                if c == "r":
                    option_list[0] = c.upper()
                    return option_list
                
                if c == "h":
                    option_list[0] = c.upper()
                    return option_list
                
                if c == "q":
                    option_list[0] = c.upper()
                    return option_list
                
                else:
                    print("Error in option:", option_list[0])
                    return None
                break
                
            if mode not in mxx:
                return None
            else:
                return option_list
            
            source = int(item[1])
            if 7 > source > 0:
                print("Error in Source.")
                return None
            else:
                return option_list
            
            destination = int(item[2])
            if 3 > destination > 0:
                print("Error in Destination")
                return None
            else:
                return option_list
         
def main():  

    the_deck = cards.Deck()  
    print("\nWelcome to Streets and Alleys Solitaire.\n")
    the_deck.shuffle()
    the_deck.display()
    
    print(MENU)

    while True:
        option_list = get_option()
        
        while True:
            if option_list == None:
                option_list = get_option()
                break
            else:
                break
            
        if "U" in option_list:
            print("Undo:")
            continue
            
        if "R" in option_list:
            print("\n- - - - New Game. - - - -\n")
            the_deck.shuffle()
            the_deck.display()
            continue
            
        if "H" in option_list:
            print(MENU)
            continue
        
        if "Q" in option_list:
            break
            
    print("Thank you for playing.")

if __name__ == '__main__':
     main()

CodePudding user response:

If you use a return statement inside a function, it will automatically break out of the function. If you need your function to continue to run you could try using a generator function or change your code so your while loop runs outside your function.

CodePudding user response:

You shouldn't break in both the if and else branches of in the while loop. You need to repeat the loop until you get a result that isn't None, and that's when you should break out.

def main():  

    the_deck = cards.Deck()  
    print("\nWelcome to Streets and Alleys Solitaire.\n")
    the_deck.shuffle()
    the_deck.display()
    
    print(MENU)

    while True:
        
        while True:
            option_list = get_option()
            if option_list is not None:
                break
            
        if "U" in option_list:
            print("Undo:")
            continue
            
        if "R" in option_list:
            print("\n- - - - New Game. - - - -\n")
            the_deck.shuffle()
            the_deck.display()
            continue
            
        if "H" in option_list:
            print(MENU)
            continue
        
        if "Q" in option_list:
            break
            
    print("Thank you for playing.")
  • Related