Reputation: 46
I have had an issue where I can't get the While loop to terminate.
userinput = ("")
while userinput != ("Search" or "Add"):
userinput = input("Search or Add?")
if userinput == "Search":
Search()
elif userinput == "Add":
print("run add request")
else: print("please choose from the following two options.")
Edit: I am sorry the changes have worked. I think after I implemented the changes I had an issue with the Shell running the previous version. Sometimes I have no idea what is happening. Thank you all again.
Edit Edit: Placed the original code back in as I did not take into account that it would confuse anyone looking for their own solution. I am quite new in terms of usage of the site. Thanks again for the help
Upvotes: 1
Views: 77
Reputation: 1
The format was not correct in the while condition statement part, You can try out this...
userinput = ("")
while userinput not in ["Search", "Add"]:
userinput = input("Search or Add?")
if userinput == "Search":
Search()
elif userinput == "Add":
print("run add request")
else: print("please choose from the following two options.")
Upvotes: 0
Reputation: 50019
The issue is with your while
test. A couple of things:
You can't use or
like this. or
needs two full conditions that resolve to true
or false
. Here you have one condition userinput != "Search"
and a string "Add"
. So it's always going to return True
since a non-zero value is always True
.
As an example:
if "Add": print("true")
>>true
Instead:
userinput != "Search" or userinput != "Add"
or
is not correct when testing two negations like !=
. One of the two conditions will always return true
. For instance if you input "Add"
then the condition userinput != "Search"
will be True
and your while
loop will continue since True or False = True
. So on and so forth. Instead you want an and
.
while userinput != "Search" and userinput != "Add":
As I suggested in my comment though, it's probably just easier to use the not in
operator on a list:
while userinput not in ['Search','Add']:
This way as your list grows your test stays nice and small/condense.
Also, while this is just my opinion, I applaud your original pre-edit code where you supplied the condition for breaking your while
loop in the while
statement instead of doing while True:
. Having had many years of bug hunting and feature adding and hotfixing, I know every time I see while True:
I'm going to be hunting through hundreds of lines of codes looking for every break
. while True:
and break
has its time and place (I imagine), but I feel like it should be an exception use-case, not the rule.
Upvotes: 4
Reputation: 1732
My solution looks like this:
userinput = ""
while userinput != "Exit":
userinput = input("Search, Add or Exit: ")
if userinput == "Search":
print("run search request")
elif userinput == "Add":
print("run add request")
elif userinput != "Exit":
print("please choose from Search, Add or Exit.")
Notes:
userinput
is initialised to ""
. It doesn't need to be a tuple, ("")
break
or continue
Upvotes: 1
Reputation: 377
Using the word break
will break you out of a while
loop, like this:
while True:
break
Place break
where you want the while loop to end.
The problem is that once the input is received, and when the input meets the end condition of the while
loop, and it isn't able to reach the code checking for the condition! Try this:
while True:
userinput = input("Search or Add?")
if userinput == "Search":
Search()
break
elif userinput == "Add":
print("run add request")
break
else: print("please choose from the following two options.")
Upvotes: 0