I have 5 different strings i am searching for in a text file. I am trying to get python to ask the user for a “room number” and if any of those numbers match, to continue, otherwise ask again until a correct number is entered.
I can get it to work if try:
roomNumber = input("Enter the room number: ")
while roomNumber != ("L1"):
roomNumber = input ("Please enter a correct room number:")
However i wish to have a positive match for L1, L2, L3, L4, and L5.
I tried:
roomNumber = input("Enter the room number: ")
while roomNumber != ("L1", "L2", "L3", "L4", "L5"):
roomNumber = input ("Please enter a correct room number:")
however this doesn’t work and i assume it wants all of those matches, not just the one.
I also tried putting each value in a ([ ]) and also tried using OR between each value but didn’t work either.
I’ve been searching for ages and can’t seem to find examples of multiple matches in a while loop.
Surely i’m missing something simple?
Python has an
in/not inoperator that is useful for things like this:It works for almost any container type in Python; all of the following are true: