【发布时间】:2021-12-12 05:14:21
【问题描述】:
我的目标是将三个数字排序为 a、b 和 c,其中 a 是最大的,b 是中间的,c 是最小的。为此,我需要编写一个函数。但是,我尝试了多种方法,但没有找到让它工作的方法。
num1 = int(input("input an integer: "))
num2 = int(input("input another integer: "))
num3 = int(input("input another integer: "))
def simple_sort(a,b,c):
if a > b and a > c:
return a
if b < a and b > c:
return b
if c < a and c < b:
return c
a,b,c=simple_sort(num1,num2,num3)
print(a,b,c)
#for it to be correct, when i do print(a,b,c), it should print num1, num2 and num3
#in ascending order
【问题讨论】:
-
一旦满足一个条件,您将返回该值。此外,您没有所有可能的结果。 . .如果我输入
1,2,3会怎样?不满足任何条件,不会返回任何内容。 -
return sorted([a,b,c], reverse=True) -
@sahasrara62 OP 在标题“不使用排序功能”中声明——大概包括
.sort和sorted -
也许然后写一个排序算法
-
geeksforgeeks.org/python-program-for-insertion-sort 一个可以实现的简单插入排序
标签: python python-3.x sorting