473,396 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

if and else clauses

i have to do a project outlined at this link:
http://pages.cpsc.ucalgary.ca/~schoc...07_CPSC_217_A2

im currently doing part 2, involving searching for information from a cat data bank and having an output of the relevent sections. i've made 5 if statements where if the data from the search key if found in the data base, it will append into a second list and finally it will print the second list with all matching criteria. this code works well, but i need to have an else clause that will print " information not found" if there are no matches. my current code is:

[code removed at posters request]

but there are many errors, it prints the else clause, then the list of matches, then the else clause again. i dont know how to format this code to make it only print the else clause if data is not found. if i remove the else statement, the program works fine. What is wrong with my current code and what can i do to fix it?
Oct 12 '07 #1
2 1475
elcron
43
An else statement is executed if the last if statement and all elif(else if in some languages) between fail. heres a commented example.

Expand|Select|Wrap|Line Numbers
  1. someCondition = input("> ")
  2.  
  3. # if a
  4. if someCondition == 1:
  5.     print 'if a'
  6.  
  7. # if b
  8. if someCondition == 2:  # is checked whether or not 'if a' failed
  9.     print 'if b'
  10. elif someCondition == 3: # is checked only if 'if b' is checked and is false
  11.     print 'elif b'
  12. else:                             # code under this statement is only executed if 'if b' and the following elif statement are both false
  13.     print 'else'
  14.  
So this code should fix your if statements:
Expand|Select|Wrap|Line Numbers
  1. #
  2.     if Key in cat[0].lower() and not cat_added: 
  3.         Output_list.append(cat) 
  4.         cat_added = 1
  5.     elif Key in cat[1].lower() and not cat_added:
  6.         Output_list.append(cat)
  7.         cat_added = 1
  8.     elif Key in cat[2].lower() and not cat_added:
  9.         Output_list.append(cat)
  10.         cat_added = 1
  11.     elif Key in cat[3].lower() and not cat_added:
  12.         Output_list.append(cat)
  13.         cat_added = 1
  14.     elif Key in cat[4].lower() and not cat_added:
  15.         Output_list.append(cat)
  16.         cat_added = 1    
  17.         print Output_list
  18.     else:
  19.         print "There is no cat matching your description"
  20.         break
  21.  
Though it would be more readable as:
Expand|Select|Wrap|Line Numbers
  1. #...
  2. success = False
  3. for i in range(5):
  4.      if Key in cat[i].lower() and not cat_added: 
  5.           Output_list.append(cat) 
  6.           cat_added = 1
  7. if not success:
  8.      print Output_list
  9. else:
  10.      print "There is no cat matching your description"
  11.  
Oct 12 '07 #2
bvdet
2,851 Expert Mod 2GB
Here's another way using a list comprehension:
Expand|Select|Wrap|Line Numbers
  1. Key = raw_input ("Enter Search Key:")
  2. Output_list = [', '.join(item) for item in Cats if Key.lower() in [s.lower() for s in item]]
  3.  
  4. if Output_list:
  5.     print '\n'.join(Output_list)
  6. else:
  7.     print 'No matching record found'
Example, enter 'LYNX':
>>> Carnivora, Felidae, Lynx, lynx, Lynx- Eurasian, 9 - 38 kg (20 - 83 lbs)
Carnivora, Felidae, Lynx, pardinus, Lynx- Iberian, 8.7 - 14.5 kg (19 - 32 lbs)
Carnivora, Felidae, Lynx, canadensis, Lynx - Canadian, 8 - 14 kg (18 - 30 lbs)
Carnivora, Felidae, Lynx, rufus, Bobcat, Red Lynx, 5.8 - 13.3 kg (13 - 30 lbs)

Example, enter 'RED LYNX':
>>> No matching record found
Oct 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
4
by: ECathell | last post by:
I had read an article at one time that suggested a pattern to get around deeply nested if..then..else hell... Can anyone point me in that direction? select case statements wont work for me in...
2
by: m.k.ball | last post by:
Thanks Rich - that's great. Before I found this group, I thought I had a reasonable understanding of SQL (well, MySQL's implementation of it, at least) but the truth is there are great chunks that...
4
by: DBC User | last post by:
Is there a software pattern for using if then else? Thanks.
25
by: metaperl.etc | last post by:
A very old thread: http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e discusses the optional "else:"...
6
by: Sonnich | last post by:
this is probably an old topic: if(a==1) if(b==2) echo "a is 1 and b is 2"; else // what happens here? echo "a is 1 and b is not 2"; The question is, where the else will react too. Usually...
5
by: Jyotirmoy Bhattacharya | last post by:
I'm a newcomer to Python. I have just discovered nested list comprehensions and I need help to understand how the if-clause interacts with the multiple for-clauses. I have this small program: ...
4
by: andersond | last post by:
I was under the impression that if you have a series of "if, else if, else if " clauses presented mutually exclusive alternatives. Apparently that is not the case. This code results in the var...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.