I have a question about handling errors. I'm still pretty early on in the course so I haven't done a whole lot of error handling or raising exceptions, so I'm not sure of the best way to go about this. Here's my code: http://codepad.org/XBx6qXz4 I'm trying to handle invalid inputs for line 5. I have 3 things I need to check for. One raises an error (non-numbers cause Decimal() to raise InvalidOperation), two don't: negative numbers, and 0. If any of these three occur, I simply want to prompt the user with an error message and then have the user try again.
The problem is I'm really not certain what the best way to handle these checks are. I'm assuming the best thing to do is handle them locally and not have my function raise errors, since I just want it to try again immediately if an invalid input occurs, and not just leave the function altogether. I don't know how I should handle it locally, though. Do I just use conditional If blocks and not even bother with exceptions? Should I raise exceptions or do something else I haven't thought of? What's the most effective way to handle this?
I think that if you don't want to use If blocks for checking and continuing, you can create a class to handle the exception, pretty much you can clean up before exiting, get the next attribute or even reissuing commands, as this example code from pythondocs: http://codepad.org/NlV9TE02 I don't know about built-in handlers though
I mean, built-in handlers to handle it locally without raising errors/exceptions and exiting the program. Might be wrong though.
I would write a separate function, like getDecimal(message) that would prompt the user with the message, and keep asking until it got a valid Decimal value. Then the return value of that function will always be a Decimal.
As a point of style, you're asking the user if they want to continue, then telling the caller to call back in to the function based on that response. You should ask that outside this function. Also, since you're only appending to the lists, and the caller has references to the lists already (or at least it should) you don't need to return the lists.
I think that creating a function is a simpler solution, even though creating a class can be more efficient when it comes to both modularity and automating the answers, but adding a function seems a lot more feasible in this case, as creating and instantiating a class would be an overkill, I think.
@dmancine: Your function idea is really good, and I think I'm going to try that. However, I'm not sure I get what you mean in your second post. Could you clarify/elaborate?
http://codepad.org/vmZ5sNEV I just changed it to how I would do it (without an Account class, that is). There are several functions that each do one thing. The foo() function coordinates their use. accountInput() just gets account data. Your accountinput() function seemed to be asking for information for several accounts, then also asking if the user wants to add more accounts. But then it didn't ask for the info for those accounts. It was passing the user's answer back to the caller, who (presumably) was just going to turn around and call accountinput() right back asking for the new account info. foo() should be asking the user if they want more accounts, or accountinput() should immediately get and return the new account info. It just seemed a little circuitous to me.
And as for the second part of my second post, names = [] names = getNames(names) # don't return names, I already have it Since names is an object (a list), getNames() receives a reference to that object, so any changes it makes to the list will be reflected in the calling scope. The caller should still have a reference to the list, so you don't need to return the reference. We say lists are "passed by reference." Things like integers and floats and strings are "passed by value." When you pass a reference, both the caller and the callee have references to the same object. When you pass a value, the callee gets a copy of that value, so the caller can't see any changes made by the callee.|dw:1318413055617:dw|In the first picture foo() gets a reference to an outside object. In the second picture bar() gets a copy of an outside object. When you're passed a reference you don't have to return it.
Join our real-time social learning platform and learn together with your friends!