On 02Mar2019 00:05, Alan Gauld <alan.ga...@yahoo.co.uk> wrote:
On 01/03/2019 19:23, Chip Wachob wrote:
I'm not sure what you mean by putting the except close to the int().
He means you should have a try/except for the int conversion,
typically something like:
# wait for it...
try: num_items = int(raw_input(": "))
except ValueError:
# try a second time.
if num_items == 0: # individual testing
print "\nIndividual testing requested"
That means the user has a chance of continuing without errors
and you don;t need to use recursion to restart the menu system.
Just further to this remark of Alan's: you should make try/except
clauses as small as is reasonable i.e. containing as little code as
possible between "try" and "except".
Not only does this have the advantage Alan mentioned, of making it
possible to offset the user the chance to continue after an error, it
_also_ means you have more confidence about what caused the exception.
In you programme, you have a try/except around the entire main body of
your loop. This means that the "except ValueError" clause will intercept
_any_ ValueError issued by the code, not just the one from the int()
call, so it needn't indicate bad user input, it could as easily indicate
some subsequent bug in your code, as many functions can raise ValueError
if they are handed something unsatisfactory.
Cheers,
Cameron Simpson <c...@cskk.id.au>
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor