473,387 Members | 1,388 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,387 software developers and data experts.

lots of nested if statements.

I'm writing a program which contains an awful lot of nested if statements. I don't know how efficient this is but it definetely makes the code less readable. Does anyone have any advice on how I can improve the following kind of code?

Apparently other languages have a switch statement which is useful here...Python seems to lack this functionality.

My Python is self-taught so I'd appreciate any outside input.

Lots of the code involves blocks of code like this:
Expand|Select|Wrap|Line Numbers
  1. for d1, d2, d3, d4, number in list:
  2.   if abs(d1-a1) < D1_tol:
  3.     if abs(d2-a2) < D2_tol:
  4.       if abs(d3-a3) < D3_tol:
  5.         if abs(d4) > abs(a4):
  6.           do_something
  7. for d1, d2, d3, d4, number in list2:
  8.   if abs(d1-a1) < D1_tol:
  9.     if abs(d2-a2) < D2_tol:
  10.       if abs(d3-a3) < D3_tol:
  11.         if abs(d4) > abs(a4):
  12.           do_something_else
  13.  
Feb 27 '07 #1
9 9310
dshimer
136 Expert 100+
If all those things have to be true at the same time how about "and", as a very simple example.

Expand|Select|Wrap|Line Numbers
  1. >>> a=1
  2. >>> b=2
  3. >>> c=3
  4. >>> if a>0 and b>0 and c>0:
  5. ...     print 'y'
  6. ... else: print 'n'
  7. ... 
  8. y
  9. >>> a=-1
  10. >>> if a>0 and b>0 and c>0:
  11. ...     print 'y'
  12. ... else: print 'n'
  13. ... 
  14. n
  15. >>> 
Feb 27 '07 #2
Thanks for the reply.

I can use 'and' sometimes. However sometimes there are so many if's that they'd go off the end of the line if I used 'and' statements. That seems even less readable.
Feb 27 '07 #3
bartonc
6,596 Expert 4TB
Thanks for the reply.

I can use 'and' sometimes. However sometimes there are so many if's that they'd go off the end of the line if I used 'and' statements. That seems even less readable.
You can break lines in Python
Expand|Select|Wrap|Line Numbers
  1. if varylongname1 and varylongname2 and varylongname2 and \
  2.     varylongname4 and varylongname5 and varylongname6:
Feb 27 '07 #4
bvdet
2,851 Expert Mod 2GB
I'm writing a program which contains an awful lot of nested if statements. I don't know how efficient this is but it definetely makes the code less readable. Does anyone have any advice on how I can improve the following kind of code?

Apparently other languages have a switch statement which is useful here...Python seems to lack this functionality.

My Python is self-taught so I'd appreciate any outside input.

Lots of the code involves blocks of code like this:
Expand|Select|Wrap|Line Numbers
  1. for d1, d2, d3, d4, number in list:
  2.   if abs(d1-a1) < D1_tol:
  3.     if abs(d2-a2) < D2_tol:
  4.       if abs(d3-a3) < D3_tol:
  5.         if abs(d4) > abs(a4):
  6.           do_something
  7. for d1, d2, d3, d4, number in list2:
  8.   if abs(d1-a1) < D1_tol:
  9.     if abs(d2-a2) < D2_tol:
  10.       if abs(d3-a3) < D3_tol:
  11.         if abs(d4) > abs(a4):
  12.           do_something_else
  13.  
This may not be better, but is a little easier to read:
Expand|Select|Wrap|Line Numbers
  1. def compare_terms(*args):
  2.     evalList = []
  3.     for item in args:
  4.         evalList.append(eval(item))
  5.     return evalList
  6.  
  7. for d1, d2, d3, d4, number in lst:
  8.     if False not in compare_terms('abs(d1-a1) < D1_tol',\
  9.                                   'abs(d2-a2) < D2_tol',\
  10.                                   'abs(d3-a3) < D3_tol',\
  11.                                   'abs(d4) > abs(a4)'):
  12.         do_whatever
I suggest not using 'list' for a variable name since it masks Python's built in function 'list()'.
Feb 27 '07 #5
bartonc
6,596 Expert 4TB
This may not be better, but is a little easier to read:
Expand|Select|Wrap|Line Numbers
  1. def compare_terms(*args):
  2.     evalList = []
  3.     for item in args:
  4.         evalList.append(eval(item))
  5.     return evalList
  6.  
  7. for d1, d2, d3, d4, number in lst:
  8.     if False not in compare_terms('abs(d1-a1) < D1_tol',\
  9.                                   'abs(d2-a2) < D2_tol',\
  10.                                   'abs(d3-a3) < D3_tol',\
  11.                                   'abs(d4) > abs(a4)'):
  12.         do_whatever
I suggest not using 'list' for a variable name since it masks Python's built in function 'list()'.
Nice catch on a keyword being used as a variable name. Very elegant coding! Thanks BV.
Feb 27 '07 #6
bvdet
2,851 Expert Mod 2GB
Nice catch on a keyword being used as a variable name. Very elegant coding! Thanks BV.
My pleasure, just glad to be here. Pushing 2,000 posts Barton? Congrats and attaboys for all your hard work!
Feb 27 '07 #7
bvdet
2,851 Expert Mod 2GB
This may not be better, but is a little easier to read:
Expand|Select|Wrap|Line Numbers
  1. def compare_terms(*args):
  2.     evalList = []
  3.     for item in args:
  4.         evalList.append(eval(item))
  5.     return evalList
  6.  
  7. for d1, d2, d3, d4, number in lst:
  8.     if False not in compare_terms('abs(d1-a1) < D1_tol',\
  9.                                   'abs(d2-a2) < D2_tol',\
  10.                                   'abs(d3-a3) < D3_tol',\
  11.                                   'abs(d4) > abs(a4)'):
  12.         do_whatever
I suggest not using 'list' for a variable name since it masks Python's built in function 'list()'.
After thinking about this again, the function is redundant:
Expand|Select|Wrap|Line Numbers
  1. for d1, d2, d3, d4, number in lst:
  2.     if False not in [abs(d1-a1) < D1_tol,\
  3.                      abs(d2-a2) < D2_tol,\
  4.                      abs(d3-a3) < D3_tol,\
  5.                      abs(d4) > abs(a4)
  6.                      ]:
  7.         do_whatever      
Feb 28 '07 #8
Thanks for everyone's replies. I'll take a closer look at your suggestion bvdet and try it out.

I was only using 'list' as an example. Gedit does a good job of highlighting reserved words to keep me making too many newbie errors like that.

Once again, thanks for all your time.
Feb 28 '07 #9
bvdet
2,851 Expert Mod 2GB
Thanks for everyone's replies. I'll take a closer look at your suggestion bvdet and try it out.

I was only using 'list' as an example. Gedit does a good job of highlighting reserved words to keep me making too many newbie errors like that.

Once again, thanks for all your time.
You are welcome. 'list' is not a reserved word like 'import', 'for', 'in', etc., but is a built-in function. 'list' can be used as a variable name. When the object reference to 'list' has decreased to zero, you would get the built-in function back.
Mar 1 '07 #10

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

Similar topics

3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
by: Jim Irvine | last post by:
Does anybody know the limit of nested iif statements you can use in Access?
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
1
by: redpayne | last post by:
Ok-I am doing homework out of a book and the instructions are to display an interface with 5 option buttons in a frame. When clicked, each button changes the background color of the frame. It...
1
by: sql_er | last post by:
Hi all, I am trying to convert an SQL statement into an XPath (or a sequence of XPath) statements. More specifically, I have the following: SELECT a FROM b WHERE c IN (SELECT d FROM e) I...
2
by: pradeep.thekkottil | last post by:
I'm setting up an auction website using PHP and MySQL. There in the section where logged in members can put up new auction in a form, I want to run a form validation where I used if else statements...
0
by: Neil Cerutti | last post by:
The docs say: A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on...
4
by: Patrick A | last post by:
All, I rely on nested IF statements with multiple conditions heavily, and someone suggested recently writing the statements (and especially reading them months later) would be much easier if I...
2
by: Ralf | last post by:
I have a custome request to print a gridview with lots of columns, 31 to be exact. I know how to print out a gridview, done it already for a GV with 6 columns. But, this GV is very wide and will...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.