how do I check if an item is in a list of items

This is the place for queries that don't fit in any of the other categories.

how do I check if an item is in a list of items

Postby mcmxl22 » Fri Feb 07, 2014 9:12 am

I am making this function as part of a program I am working on for practice. There is more code to it than I posted here. I wanted to keep it short.
What I want to know is how to get the if statement to print "This room is occupied. The door is locked" when doors 1, 2, 3, and 5 are entered without having 5 elif statements.

Code: Select all
def stairs():
    print "You find yourself in the up stairs hall."
    print "There are 6 doors. Which one do you open?"
    door = ['1', '2', '3', '4', '5', '6']
    for num in door:
        print "\t%s" % num
      
    room = int(raw_input(">"))
   
    if room == door[0]:
        print "This room is occupied. The door is locked."
        stairs()

    else:
        living_room()
Last edited by Yoriz on Fri Feb 07, 2014 6:38 pm, edited 2 times in total.
Reason: Locked, Changed title.
mcmxl22
 
Posts: 2
Joined: Thu Feb 06, 2014 7:27 am

Re: how do I get the same answer to multiple list items

Postby stranac » Fri Feb 07, 2014 10:36 am

Just use in:
Code: Select all
if room in {'1', '2', '3', '5'}:
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: how do I get the same answer to multiple list items

Postby mcmxl22 » Fri Feb 07, 2014 6:16 pm

Thank you for the help. It's working.
mcmxl22
 
Posts: 2
Joined: Thu Feb 06, 2014 7:27 am


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 2 guests