# reads in 2 integers, prints their greatest common divisor # Matt Bishop, MHI 289I, Fall 2022 # # read in a positive number # caller will handle any exceptions # we have to raise non-positive exception # def getnum(what): # read in the number (you hope) # if it's not a number, the ValueError exception is # raised automatically n = int(input("Please enter the %s number: " % what)) # it's a number; now we raise an exception if it's # not positive if n <= 0: raise ValueError("Integer must be positive") # positive; return it return n # # recursively compute the GCD of two numbers # parameters: m, n are the numbers # returns: the number of positive integers in the list # def gcd(m, n): # base case: if n is 0, the other is the GCD if n == 0: return m # recursion here # this just is the Euclidean algorithm else: return gcd(n, m % n) # # get the inputs, handle errors, and report results # def main(): # read in two numbers try: m = getnum("first") n = getnum("second") # say what went wrong, if anything, and then quit except ValueError as errmsg: print("Bad number:", errmsg) return except Exception as errmsg: print(errmsg) return # now print the results print("The greatest common divisor of", m, "and", n, "is", gcd(m, n)) # now run the program main()