Exercise 10.1

What does the following code do with various inputs?

try:
    f = int(input('numerator: ')) / int(input('denominator: '))
    print(f)
except ZeroDivisionError as e:
    print('fail...')

Exercise 10.2

Consider the following code.

class Fraction:
    
    def __init__(self, numerator=0, denominator=1):
        if denominator == 0:
            print('naughty, naughty...')
            sys.exit(-1)
        self.numerator = numerator
        self.denominator = denominator
    
    def get_float_value(self):
        return self.numerator / self.denominator
        
if __name__ == '__main__':
    f = Fraction(int(input('numerator: ')), int(input('denominator: ')))
    print(f)

Modify the code to handle problems more gracefully.

Exercise 10.3

Consider the following code, which continues to use the same fraction class.

f = open(input('Fractions filename: '))
for line in f.readlines():
    tokens = line.strip().split(' ')
    f = Fraction(int(tokens[0]), int(tokens[1]))
    print(f.get_float_value())

Modify the code to handle potential errors more gracefully.

Exercise 10.4

Consider the following test code, placed in the main script of the fraction module.

f = Fraction(1, 1)
assert f.get_float_value() == 1.0

Try to do the following.