python 三个数字比较大小,判断大小,按照大小顺序输出

1.利用条件判断语句(if-elif-else)两两比较

# 提醒用户输入三个整数,通过int()方法将str类型转换成int类型  
number_one = int(input("请输入一个整数"))  
number_two = int(input("请输入一个整数"))  
number_three = int(input("请输入一个整数"))  
# 判断第一个数字是否大于第二个数字  
if number_one > number_two:  
    # 条件成立判断第二个数字是否大于第三个数字  
    if number_two > number_three:  
        print("{}>{}>{}".format(number_one, number_two, number_three))  
    elif number_two < number_three:  
        if number_one > number_three:  
            print("{}>{}>{}".format(number_one, number_three, number_two))  
        elif number_one < number_three:  
            print("{}>{}>{}".format(number_three, number_one, number_two))  
elif number_one < number_two:  
    if number_one > number_three:  
        print("{}>{}>{}".format(number_two, number_one, number_three))  
    elif number_one < number_three:  
        if number_three > number_two:  
            print("{}>{}>{}".format(number_three, number_two, number_one))  
        elif number_three < number_two:  
            print("{}>{}>{}".format(number_two, number_three, number_one))  

2.将三个数字添加到列表中,再通过max函数进行大小比较

# 利用列表的max()函数来比较大小
numbers = []
for i in range(3):
    number = int(input("请输入第{}个整数".format(i+1)))
    numbers.append(number)
print(numbers)
for i in range(len(numbers)):
    max_number = max(numbers)
    print("{}>".format(max_number), end="")
    numbers.remove(max_number)

3.利用列表的sort函数对数字进行排序并通过reverse将数字通过降序输出

# 利用sort排序来进行比较
numbers = []
for i in range(3):
    number = int(input("请输入第{}个整数".format(i+1)))
    numbers.append(number)
print(numbers)
numbers.sort(reverse=True)
print("{}>{}>{}".format(numbers[0], numbers[1], numbers[2]))
    原文作者:黄桥烧饼
    原文地址: https://blog.csdn.net/abiao7722882/article/details/93718658
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞