Check if an item is in a list or equals another item

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

Check if an item is in a list or equals another item

Postby ishaan » Mon Mar 18, 2013 8:17 pm

I am trying to check whether the input matches either of the fruits in the list or '1'
It works (prints "Correct!" and exits) if I type in '1' and press enter. However, when I type in 'apples' (for example, or any other fruit) it doesn't work and the loop repeats asking "Enter a fruit:".
It obviously works when i replace
Code: Select all
if fru in [fruits, '1']
with
Code: Select all
 if fru in fruits or fru=='1'
But that's not how I want to write the code. I want the code to be as short as possible :)

Here is my code -
Code: Select all
from sys import exit
fruits = ['apples','oranges','bananas','mangos','strawberries']

def checkfruit():
   fru = raw_input("Enter a fruit: ")
   if fru in [fruits, '1']:
      print "Correct!"
      exit(0)
   else:
      checkfruit()

      
checkfruit()


Any help would be appreciated! I couldn't find "in" anywhere in Python docs, or maybe I didn't check them correctly. Just started with python yesterday hehe.
Last edited by Yoriz on Tue Mar 19, 2013 1:24 pm, edited 1 time in total.
Reason: Changed the title, original was 'if i in list'
ishaan
 
Posts: 3
Joined: Mon Mar 18, 2013 8:07 pm

Re: if i in list

Postby micseydel » Mon Mar 18, 2013 8:36 pm

The way you've found that it works is the shortest. We use the
Code: Select all
if user_in in (option1, option2, option3, ...):

trick in place of checking for equivalency with each thing,
Code: Select all
if user_in == option1 or user_in == option2 or user_in == option3 or ...:

But if you need to to check if it's in the list or equal to something, unless you want to add that something to the list ("1" to fruits here) then you have to write it out.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1270
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: if i in list

Postby ishaan » Mon Mar 18, 2013 8:57 pm

Thanks a lot there! That was helpful. I'll play around with "in" keyword for a while to get the hang of it :D
Cheers!
ishaan
 
Posts: 3
Joined: Mon Mar 18, 2013 8:07 pm

Re: if i in list

Postby setrofim » Mon Mar 18, 2013 8:59 pm

ishaan wrote:I couldn't find "in" anywhere in Python docs

Yeah, it's a bit tricky to find. It is explained in the section on comparisons: http://docs.python.org/2/reference/expressions.html#membership-test-details
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: if i in list

Postby ishaan » Mon Mar 18, 2013 9:31 pm

Thanks for the link! :D
ishaan
 
Posts: 3
Joined: Mon Mar 18, 2013 8:07 pm

Re: if i in list

Postby micseydel » Mon Mar 18, 2013 9:32 pm

Yeah, thanks setrofim, I looked and couldn't find it!
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1270
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: if i in list

Postby empeeu » Mon Mar 18, 2013 9:36 pm

Try this:
Code: Select all
from sys import exit
fruits = ['apples','oranges','bananas','mangos','strawberries']

def checkfruit():
   fru = raw_input("Enter a fruit: ")
   if fru in (fruits + ['1']):
      print "Correct!"
      exit(0)
   else:
      checkfruit()

     
checkfruit()


I think the problem is you are comparing your input string to [['apples', ...], '-1'], and "apples" certainly is not equal to the entire list. :)
empeeu
 
Posts: 3
Joined: Wed Mar 06, 2013 7:11 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests