Python: string formatting and calling functions
Question: So I'm running in to the string formatting error when trying to pass the
arguments num1 and num2 to the function gcd. I'm not sure how to fix this.
Please bear with me since I'm new to Python programming. Thanks!

    
    
    #!/usr/bin/python
    import sys
    from collections import defaultdict
    
    lines = sys.stdin.read()
    lineArray = lines.split()
    listLength = len(lineArray)
    
    def gcd(a, b):
        c = 0
        if a > b:
            r = a%b
            if r == 0:
                return b
            else:
                return gcd(b, r)
        if a < b:
            c = b
            b = a
            a = c
            return gcd(a, b) 
    
    for x in range(0, listLength):
            num1 = lineArray[x]
            num2 = lineArray[x+1]
            print num1, 'and', num2
            print gcd(num1, num2)
    
        print 'end'
    


Answer: It's pretty simple. `lineArray` is not a list containing integers, but
strings. So when you do this:

    
    
    r = a%b
    

It tries to format the string `a`, instead of calculating `a%b`. To solve
this, convert `a` and `b` to integers:

    
    
    def gcd(a, b):
        a,b = int(a),int(b)
        c = 0
        if a > b:
            r = a%b
            if r == 0:
                return b
            else:
                return gcd(b, r)
        if a < b:
            c = b
            b = a
            a = c
            return gcd(a, b)
    

Also, in your `gcd` function, the recursion never ends. Hint: You'll have to
check if b is 0. Hope this helps!

