Hal Lawrence Hal Lawrence - 1 month ago 4
Python Question

Returning values into a tuple?

Struggling trying to create a function def of how to return three accepted integers and into a tuple in a sorted format from least to greatest depending if ascending is true. How would I go about coding this out? Would I use a for loop with if statements whether its ascending? I have a general idea for the sorting using "sorted(list)" and "sorted(list, reverse=True)."

Function code I have so far:

def sort3(x,y,z, ascending=True):
list(x,y,z)
return lst


Test should be like this sort3(5,3,4) returns (3,4,5)

Answer

This should do the trick:

def sort3(x, y, z, ascending=True):
    return tuple(sorted([x, y, z], reverse=not ascending))
Comments