lundi 20 juillet 2020

Loop an if statement if the else condition is met

So I have an If-elif statement that I want to print some text and loop if the else condition is met. Here is the code:

print("Search Options:\n1. s - Search by keyword in general\n2. u - Search for specific user data\n3. kwin - Search a keyword in a specific user\n4. allin - Search for all data by and mentioning a user")

search_mode = input("How would you like to search?: ")

if "s" in search_mode:
    kwsearch = input("What Keyword do you want to use?: ")
elif "u" in search_mode:
    username = input("What is the username?: ")
elif "kwin" in search_mode:
    kwinuser = input("What is the username?: ")
    kwinword = input("What is the keyword?: ")
elif "allin" in search_mode:
    allinuser = input("What is the username?: ")
else:
    print("Error. Please check spelling and capitalization")

When people mess up and don't put one of the options properly, I want to loop back to the if statement so that when they put in the right one, the loop will end and the rest of the code will continue.

I tried a for loop and wrapped it all as a function but it would end up in an infinite loop of printing the error message. Is there a way to do it with a while loop? Do I need to block it ad a function to repeat it?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire