简体   繁体   中英

How do i write an algorithm that reads three numbers and prints them out in ascending order in python

How do i write an algorithm that reads three numbers and prints them out in ascending order in python. Please help this is what i have tried so far i hope you guys can help me out thank you, this is using python and i am new to programming. I do not know how to do the part where it says in ascend orders.

one = float(input("Please input a number : "))
two = float(input("Please input a second number : "))
three = float(input("Please input a third number : "))

if one > two and three:
    print("")*

Based on your code, the following one, will print in order the numbers one and two in ascending order. I suggest you try to complete the code for three elements.

one = float(input("Please input a number : "))
two = float(input("Please input a second number : "))
three = float(input("Please input a third number : "))

if one < two:
    print(one, two)
else:
    print(two, one)

Your move...

you can use sorted(), but first you have a save the numbers in a list, try with this:

nums = []
one = nums.append(float(input("Please input a number : ")))
two = nums.append(float(input("Please input a second number : ")))
three = nums.append(float(input("Please input a third number : ")))
for num in sorted(nums):
    print (num)

sorted() - Python Doc

If you need use a if and else statement, you should think in all combinations about your input numbers and evaluate this, try with this:

one = float(input("Please input a number : "))
two = float(input("Please input a second number : "))
three = float(input("Please input a third number : "))

if one < two and three < two:
    if one > three:
        print(three, one, two)
    else:
        print(one, three, two)
elif one < three and two < three:
    if one > two:
        print(two, one, three)
    else:
        print(one, two, three)        
elif two < one and three < one:
    if three > two:
        print(two, three, one)
    else:
        print(three, two, one)
else:
    print(one, two, three)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM