I have the following simple function:
def divide(x, y):
quotient = x/y
remainder = x % y
return quotient, remainder
x = divide(22, 7)
<
You are essentially returning a tuple, which is an iterable we can index, so in the example above:
print x[0]
would return the quotient and
print x[1]
would return the remainder
You have two broad options, either:
Modify the function to return either or both as appropriate, for example:
def divide(x, y, output=(True, True)):
quot, rem = x // y, x % y
if all(output):
return quot, rem
elif output[0]:
return quot
return rem
quot = divide(x, y, (True, False))
Leave the function as it is, but explicitly ignore one or the other of the returned values:
quot, _ = divide(x, y) # assign one to _, which means ignore by convention
rem = divide(x, y)[1] # select one by index
I would strongly recommend one of the latter formulations; it's much simpler!
You can either unpack the return values when you call your method:
x, y = divide(22, 7)
Or you can just grab the first returned value:
x = divide(22, 7)[0]