Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading