簡體   English   中英

我如何編寫一個算法,該算法讀取三個數字並在python中按升序將其打印出來

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

我該如何編寫一種算法,該算法讀取三個數字並在python中以升序打印出來。 請幫助這是到目前為止我一直在嘗試的方法,希望大家能幫助我,謝謝,這是使用python的方法,我是編程新手。 我不知道該如何按升序說明該部分。

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("")*

根據您的代碼,下面的代碼將按升序順序打印數字1和2。 我建議您嘗試完成三個元素的代碼。

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)

你的舉動...

您可以使用sorted(),但首先需要將數字保存在列表中,然后嘗試以下操作:

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文檔

如果需要使用if and else語句,則應考慮所有組合使用您的輸入數字並對此進行評估,請嘗試以下操作:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM