Python -如何让数字总是按升序打印

gfttwv5a  于 2023-02-17  发布在  Python
关注(0)|答案(1)|浏览(120)

我正在写一些总是按升序打印数字的东西。
我有类似下面这样的东西,但它总是打印的顺序,我有变量的数字。我知道我错过了一些东西,使它,使他们将打印在升序,即使我扔在更多的数字,所以他们总是排序。我只是不知道该添加什么。

# This function compares two numbers and returns them
# in increasing order.
def order_numbers(number1, number2):
    if number2 > number1:
        return number1, number2
    else:
        return number2, number1

smaller, bigger = order_numbers(100, 99)
print(smaller, bigger)

我知道我可以写类似print(sorted([smaller, bigger]))的东西,但我不希望它像这样打印
edit1:print也是如此(order_numbers(较小,较大))

5uzkadbs

5uzkadbs1#

def order_numbers(*args):
    numbers = list(args)
    numbers.sort()
    return numbers

# the * here unpacks the list, so you don't the the square brackets of list output.
print(*order_numbers(100,21,32,12))
# 12 21 32 100

print(*sorted(number_list))

相关问题