Python: placing statements behind each other

The following is possible.

try: 
    if minX<minBbX: minBbX=minX
except: minBbX=minX

This is not possible, or is it possible?

try: if minX<minBbX: minBbX=minX
except: minBbX=minX

In Python, the try block is meant to test a portion of code for errors. The except block lets you handle the error.

Example:

try:
    print x
except:
    raise ValueError("The variable x is not defined")

I don’t get what you are trying to do here, but wouldn’t this be feasible with a simple if statement? Your code logic seems a bit redundant. If minX is smaller than minBbX, minBbX becomes minX, otherwise minBbX also equals minX, which could simply be written like this:

minBbX = minX

Btw, both code blocks that you posted above are pretty much exactly the same. O.o

2 Likes

Yes and it is always preferable to trap specific exceptions if possible, ie:

try: 
    print x 
except NameError:
    raise ValueError("The variable x is not defined")

Because:

https://realpython.com/the-most-diabolical-python-antipattern/

2 Likes