Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 18th, 2013, 03:25 PM
ishaan ishaan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 2 ishaan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 15 sec
Reputation Power: 0
If i in list

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:
if fru in [fruits, '1']
with
Code:
 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:
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.

Reply With Quote
  #2  
Old March 18th, 2013, 04:22 PM
codergeek42 codergeek42 is offline
Brony & F/OSS Advocate
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Jul 2003
Location: Anaheim, CA (USA)
Posts: 6,638 codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)  Folding Points: 39542 Folding Title: Starter FolderFolding Points: 39542 Folding Title: Starter Folder
Time spent in forums: 1 Month 2 Weeks 6 Days 1 h 33 m 45 sec
Reputation Power: 2474
Send a message via ICQ to codergeek42 Send a message via AIM to codergeek42 Send a message via Yahoo to codergeek42 Send a message via Google Talk to codergeek42
Facebook
The first thing that comes to mind is to make them both sets and then check if '1' is in their union:
python Code:
Original - python Code
  1. if fru in set(fruits) | set(['1']):
  2.     print("Correct!")
Alternatively, you could flatten the [fruits, '1'] list by calling into the compiler.ast module, and use that:
python Code:
Original - python Code
  1. from compiler.ast import flatten
  2.  
  3. # ...
  4.  
  5. if fru in flatten([fruits, '1']):
  6.     print("Correct!")
To be quite frank though, I feel that your original if statement is the ideal one:
python Code:
Original - python Code
  1. if fru in fruits or fru=='1':
  2.     print("Correct!")
Keep in mind The Zen of Python, particularly "Explicit is better than implicit" and "Readability counts". In your original if statement, it's very clear to see what the conditions are; whereas in the other two, even if it does the check against only one list (or set), it takes a bit more thought to see what the conditions are and when it would be true/false.
__________________
~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )

Reply With Quote
  #3  
Old March 19th, 2013, 02:59 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 55 sec
Reputation Power: 65
Quote:
Originally Posted by codergeek42
The first thing that comes to mind is to make them both sets and then check if '1' is in their union:


Well sure, but it’s just as easy to use list concatenation:

Code:
if fru in fruits + ['1']:


Quote:
Alternatively, you could flatten the [fruits, '1'] list by calling into the compiler.ast module, and use that:


I have no idea where you got that compiler.ast thingy–seems not be present in my Python installations, nor is it documented in docs.python.org!
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #4  
Old March 19th, 2013, 03:44 AM
ishaan ishaan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 2 ishaan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 15 sec
Reputation Power: 0
Thumbs up

Thanks a lot for your help guys. That was really well elaborated
Gotta get play around with lists for a while

Reply With Quote
  #5  
Old March 23rd, 2013, 02:50 PM
codergeek42 codergeek42 is offline
Brony & F/OSS Advocate
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Jul 2003
Location: Anaheim, CA (USA)
Posts: 6,638 codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)  Folding Points: 39542 Folding Title: Starter FolderFolding Points: 39542 Folding Title: Starter Folder
Time spent in forums: 1 Month 2 Weeks 6 Days 1 h 33 m 45 sec
Reputation Power: 2474
Send a message via ICQ to codergeek42 Send a message via AIM to codergeek42 Send a message via Yahoo to codergeek42 Send a message via Google Talk to codergeek42
Facebook
Quote:
Originally Posted by SuperOscar
Well sure, but it’s just as easy to use list concatenation: [..]
I forgot you could do that with lists...Derp. Thanks.


Quote:
I have no idea where you got that compiler.ast thingy–seems not be present in my Python installations, nor is it documented in docs.python.org!
Hmm...that's odd. I did a quick Google search and found it mentioned on some other forums. On my Fedora system, it's part of the core install, so...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > If i in list

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap