Print a list in reverse order with range()?

use reversed() function: It’s much more meaningful. Update: If you want it to be a list (as btk pointed out): Update: If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step) For example, to generate a list [5,4,3,2,1,0], you can use the following: It may be less intuitive but as … Read more

Asking the user for input until they give a valid response

The simplest way to accomplish this is to put the input method in a while loop. Use continue when you get bad input, and break out of the loop when you’re satisfied. When Your Input Might Raise an Exception Use try and except to detect when the user enters data that can’t be parsed. Implementing Your Own Validation Rules If you want to reject values … Read more

python exception message capturing

You have to define which type of exception you want to catch. So write except Exception, e: instead of except, e: for a general exception (that will be logged anyway). Other possibility is to write your whole try/except code this way: in Python 3.x and modern versions of Python 2.x use except Exception as e instead of except Exception, e:

Python Error: AttributeError: __enter__ [duplicate]

More code would be appreciated (specifically the ParamExample implementation), but I’m assuming you’re missing the __enter__ (and probably __exit__) method on that class. When you use a with block in python, the object in the with statement gets its __enter__ method called, the block inside the with runs, and then the __exit__ gets called (optionally with exception info if one was raised). Thus, if you don’t have an __enter__ defined … Read more