-1
things = ["shorts", "pen", "pencil"]
userthings = input("what do you want to find?")
userthings = userthings.lower()
bagged = False
for i in things:
    if i == userthings:
        bagged = True

if bagged == True:
    print ("I has thing")
else:
    print ("I dont has thing")        

morestuff = input("Do you want to add anything else?")
morestuff = morestuff.lower()

while (morestuff == "yes" or "y"):
    stuff = input("What do you want to add?")
    str(stuff)
    things.append(stuff)
    print ("I have the following things:", things)
    morestuff = input("Do you want to add anything else?")
else:
    print ("Lol bye")

My problem is the while statement code, when I input "no" into morestuff, or anything other than "yes" or "y" it doesn't print "Lol bye" but carries on with the stuff = input("What do you want to add?") statement and goes in an infinite loop.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Panda God
  • 11
  • 3
  • The reason for your error is that Python sees `morestuff == "yes" or "y"` as `(morestuff == "yes") or ("y") `. Unless you type "yes", `(morestuff == "yes")` will evaluate to `False`, and you are left with `False or "y"`, which evaluates to `"y"`. When `"y"` is treated as a Boolean, it evaluates to `True`. If you type "yes", `True or "y"` evaluates to `True` as well. So your loop will continue forever. – Galax Nov 23 '15 at 23:36
  • Even if you had tried something like `morestuff == ("yes or "y")` it still wouldn't have worked properly though, because `"yes" or "y"` evaluates to simply `"yes"`, so only an input of "yes" would have made your code continue. – Galax Nov 23 '15 at 23:41

1 Answers1

0

y will always evaluate to True and mean that your loop never ends. Instead do

while (morestuff == "yes" or morestuff == "y"):

or more idiomatically

while (morestuff in ("y", "yes")):
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61