Hal Lawrence Hal Lawrence - 3 years ago 107
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):
return lst

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

Answer Source

This should do the trick:

def sort3(x, y, z, ascending=True):
    return tuple(sorted([x, y, z], reverse=not ascending))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download