[Tutor] Terminology question

Jim Mooney Py3.4.3winXP cybervigilante at gmail.com
Fri May 15 00:43:30 CEST 2015


I noticed that if I call a function that throws an error, I can catch it
from the caller, instead of catching it in the function. Is this is what is
known as "errors bubbling up?" Also, is this how you're supposed to do it?

*** Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600
32 bit (Intel)] on win32. ***


'''Print a table of random numbers, given min, max, rows, and columns'''
from random import randint


def make_table(minimum, maximum, rows, columns):
    for row in range(rows):
        print()
        for column in range(columns):
            print(randint(minimum, maximum), end = ' ')


def get_input():
    inp = input("Enter minimum, maximum, rows, and columns, separated by
commas: ")
    inps = inp.split(',')

    try:
        minimum = int(inps[0])
        maximum = int(inps[1])
        rows = int(inps[2])
        columns = int(inps[3])
        return minimum, maximum, rows, columns
    except ValueError as err:
        print("non-integer entered", err)
        return None
    except IndexError as err:
        print("You didn't enter enough values.", err)
        return None

vals = get_input()
if vals:
    minimum, maximum, rows, columns = vals
    try:
        make_table(minimum, maximum, rows, columns)
    except ValueError as err: # CATCH FUNCTION ERROR HERE INSTEAD OF IN
FUNCTION
        print("Enter min before max.")
else:
    print('Nothing to do.')

-- 
Jim


More information about the Tutor mailing list