Search code examples
pythondefinition

Why is my program not printing correctly?


The explanation is below:

def displaySortedNumbers(num1, num2, num3):
    smallest = num1
    if num2 < smallest:
        smallest = num2
    if num3 < smallest:
        smallest = num3

    return smallest

def main():
    num1, num2, num3 = eval(input("Enter three numbers seperated by commas:"))
    print("The numbers are,",displaySortedNumbers(num1, num2, num3))

main()

After the three numbers are entered, the smallest number prints out but the rest of the numbers do not follow. I need the numbers to print out from smallest to largest. I'm not sure what I did wrong.


Solution

  • In your return statement there is only ´smallest´, not the other variables.

    You can store the values in a list, sort it and then return that list, just like this

    def displaySortedNumbers(num1, num2, num3):
        list = [num1, num2, num3]
        list.sort()
        return list