0

In my class we are attempting to use python to loop simple programs and ask the user if they want to continue. Initially i devised this simple line:

answer = input("Do you want to continue? \n")
if "n" or "N" in answer:
    cont = False

However it is treating any string of characters input into "answer" as though they contain "n" or "N". At the top of the program is "while cont:" which ensures the program loops otherwise. If i take out the user input it will loop indefinitely. With the user prompt written as it is it will always break.

Why does it behave like this? Is it possible to look over a string and return a particular response without having to enter a precise string?

toxxi
  • 3
  • 1
  • 1
    Note in this case you could also use `if 'n' in answer.lower():`. – jonrsharpe Mar 27 '17 at 08:50
  • 1
    `"n" or "N" in answer` is evaluated as `bool("n") or bool("N" in answer)`, and `bool("n")` is always `True`, that is the reason, you should use `"n" in answer or "N" in answer` or th code suggested by @jonrsharpe but note that the loop will stop also when the user put `asdnasd` as an input, so you may want `answer.lower() == 'n'` more – Paweł Kordowski Mar 27 '17 at 09:09

0 Answers0