0

I'm working on a Python project to get better with passing parameters into functions, so built a small program that takes user input and appends it to a list.

I've got most of that working, but I'm stuck on trying to allow a user to add another input if they want. The program asks if they'd like to add another input, with an if/else to handle a "Y" or "N" answer. That's working for the most part. The problem comes when a user enters an invalid response (anything other than a non-case sensitive 'y' or 'n'). At this point, the program is just displaying the goodbye message and ending. I think it has to do with my while loop, since it's supposed to break out when the new_input() function isn't resolving to true anymore. I want that function to continue to cycle through until the user enters a valid response, which would then cause it to continue and take another input or end appropriately.

Is there a way to get this to work with my combination of an if/else statement or is there another method that would work better? I'll drop the relevant pieces of the program below.

def new_input():
    answer = input("Would you like to add more wisdom?(y/n): ")
    if answer == "y" or answer == "Y":
        return True
    elif answer == 'n' or answer == 'N':
        return False
    else:
        print("Invalid input. Please try again!") 

def main():
    name = input('Please enter your name:')
    take_input(name)
    while new_input():
        take_input(name)
        print_lib()
    print("Thanks for your brain cells, buddy! Bye bye!")

Thanks for your help!

1 Answers1

0

You can always return True, unless N/n is pressed, this way the program will continue running:

def new_input():
    answer = input("Would you like to add more wisdom?(y/n): ")
    if answer == "y" or answer == "Y":
        return True
    elif answer == 'n' or answer == 'N':
        return False
    else:
        print("Invalid input. Please try again!") 
        return True
ahmed
  • 5,430
  • 1
  • 20
  • 36