| | |
How does python evaluate conditional statements?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
(a & b):
What happens with logical and? If a is false, does python evaluates b or not?
python Syntax (Toggle Plain Text)
>>> def a(): ... print 'a' ... return False ... >>> def b(): ... print 'b' ... return True ... >>> a() and b() a False >>> b() and a() b a False >>>
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
The reason I asked about the evaluation of the logical and was because of this error I keep getting.
Here's the code:
Here's the error:
According to what was mentioned before, I can't understand why this error occurs.
Python should only evaluate objx.group() == m if objx != None is True, since it is False it should not evaluate it and this error would not occur.
To overcome this I nested two if statements, like so:
Even though this last piece of code works I still would like to know why the error occurs.
Thanks.
Here's the code:
python Syntax (Toggle Plain Text)
import re m = "03-AA-12" x = re.compile("[A-Z]{2,2}-[0-9]{2,2}-[0-9]{2,2}") objx = x.match(m) if (objx != None) & (objx.group() == m): print "yes" else: print "no"
Here's the error:
Traceback (most recent call last):
File "D:\Workspace\py.LAB\src\lab.py", line 9, in <module>
if (objx != None) & (objx.group() == m):
AttributeError: 'NoneType' object has no attribute 'group'According to what was mentioned before, I can't understand why this error occurs.
Python should only evaluate objx.group() == m if objx != None is True, since it is False it should not evaluate it and this error would not occur.
To overcome this I nested two if statements, like so:
python Syntax (Toggle Plain Text)
if objx != None: if objx.group() == m: print "yes"
Even though this last piece of code works I still would like to know why the error occurs.
Thanks.
![]() |
Similar Threads
- Starting Python (Python)
- help with while loop (Python)
- Creating a Calculator using C++ (C++)
- tolower()'d cstrings in conditional statements (C++)
- here it is (Python)
- Conditional Expression (Python)
- new programmer stuck with 'if statements.. (C++)
Other Threads in the Python Forum
- Previous Thread: self programming code
- Next Thread: print string w/o translating special characters
Views: 470 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
anti array avogadro beginner builtin clear client code color count csv curved def dictionary dynamic enter examples excel file float format frange ftp function gui heads hints homework import input java lapse line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random raw_input recursion recursive redirect script scrolledtext singleton software sqlite ssh stderr string strings subprocess sum syntax table terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wx.wizard wxpython






