Ethan Perrin Ethan Perrin - 1 month ago 7
Python Question

Making the final product of one function play off the final product of a different function

I want to write a code where it randomly generates numbers and can then subtract those numbers for a solution. If I had a code like this:

def rand1():
rand1 = random.choice('123456789')
print rand1

def rand2():
rand2 = random.choice('987654321')
print rand2


Is there a way I could do
rand1() - rand2()
? Or any suggestions on how I could make them play off one another. Thanks

Answer

Instead of having print, if you do return you can do that. You also need to convert the str to an int using int().

Your methods should look like this instead

def rand1():
        rand1 = random.choice('123456789')
        return int(rand1)
def rand2():
        rand2 = random.choice('987654321')
        return int(rand2)