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 in
operator that is useful for things like this:
while roomNumber not in ("L1", "L2", "L3", "L4", "L5"):
...
It works for almost any container type in Python; all of the following are true:
1 in [1, 2, 3]
3 in range(50)
"foo" in { "foo": "bar" }
"bar" not in { "foo": "bar" } # (it only looks at keys for dictionaries)
Use in
:
while not roomNumber in ("L1", "L2", "L3", "L4", "L5"):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.