0

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!

asked Jul 21, 2020 at 2:45

2 Answers 2

1

In Python, the most idiomatic thing I see for this is while True:

while True:
 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")
 continue
 break
answered Jul 21, 2020 at 2:49
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a while loop and break statement. You can also reduce the elif statement as I see duplicate code.

Also, you can reduce the user error by converting the search_mode to lowercase.

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?: ")
while True:
 if "s" in search_mode.lower():
 kwsearch = input("What Keyword do you want to use?: ")
 break
 elif search_mode.lower() in ('u','kwin','allin'):
 username = input("What is the username?: ")
 if "kwin" in search_mode.lower():
 kwinword = input("What is the keyword?: ")
 break
 else:
 print("Error. Please check spelling and capitalization")
 search_mode = input("How would you like to search?: ")

The code enters the while loop after accepting value into variable search_mode.

If the value is 's', it asks for the keyword and breaks the loop.

if the value is not 's', then it checks if the value is 'u' or 'kwin' or 'allin'. If it is any of these, then it asks for the username. If the value is kwin, it also asks for keyword. Then it breaks the loop.

If the value is none of the above, it prints the error statement and asks the user the question again. It goes into the loop again with the new value from the user and checks the conditions again. It will exit only when the if or elif statement is true. Hope this helps.

answered Jul 21, 2020 at 3:12

2 Comments

You did try to loop just the if statement which is what I said. I also appreciate the suggestion for the code condensing, but I need those two to be separate for a portion of the code later and the inclusion of the first question being asked again was key. Also, I don't know if this was due to the code condensing or the placement of the loop but what you gave put me in an infinite loop that just displayed the else condition.
you can remove the break statement from the elif and you will keep looping until your answer is 's'. Is that what you want? Looks like thatws what you want. I have explained my code in my response.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.