打印用户通过条件语句输入的三个变量中的最大和第二大数字

问题描述 投票:0回答:7

嘿嘿,我最近开始学习Python。下面是查找最大数字的程序,我想知道如何打印第二大数字。预先感谢。

x=int(input("Enter the 1st number: \n"))
y=int(input("Enter the 2nd number: \n"))
z=int(input("Enter the 3rd number: \n"))

if x>y:
    f1=x
else:
    f1=y

if y>z:
    f2=y
else:
    f2=z
    
if f1>f2:
    print(str(f1)+" is greatest")
else:
    print(str(f2)+" is greatest")
python if-statement conditional-statements
7个回答
2
投票

通过将输入添加到列表并对其进行排序,您可以轻松获得第二大数字。

试试这个:

xyz = [x,y,z]
sorted_xyz = sorted(xyz)
largest = sorted_xyz[-1]
second_largest = sorted_xyz[-2]

因为您希望它与条件语句一起使用。你可以尝试:

# check if x is the largest
if ( x >= y and x >= z ):
    print(str(x) + " is the greatest")
    # check if y is greater than z, if so then it is the second largest number.
    if ( y >= z ):
        print(str(y) + " is second greatest")
    else:
        print(str(z) + " is second greatest")

# check if y is the largest
if ( y >= x and y >= z ):
    print(str(y) + " is the greatest")
    # check if x is greater than z, if so then it is the second largest number.
    if ( x >= z ):
        print(str(x) + " is second greatest")
    else:
        print(str(z) + " is second greatest")

# check if z is the largest
if ( z >= x and z >= y ):
    print(str(z) + " is the greatest")
    # check if x is greater than y, if so then it is the second largest number.
    if ( x >= y ):
        print(str(x) + " is second greatest")
    else:
        print(str(y) + " is second greatest")

2
投票

使用

min
max
,它们是 python 的内置函数

x = 12
y = 5
z = 3
second_max = min(x, max(y, z))
print(second_max)

5

无论数值如何,您总是会得到第二个最大值。


1
投票

你可以这样做:

x=int(input("Enter the 1st number: \n"))
y=int(input("Enter the 2nd number: \n"))
z=int(input("Enter the 3rd number: \n"))

numbers_in_ascending_order = sorted([x,y,z])

numbers_in_descending_order = numbers_in_ascending_order.reverse()

second_largest = numbers_in_descending_order[1]

print("second largest:"+second_largest.__str__())

或者你可以尝试这个:

x=int(input("Enter the 1st number: \n"))
y=int(input("Enter the 2nd number: \n"))
z=int(input("Enter the 3rd number: \n"))
lst = [x,y,z]
lst.sort(reverse=True)
second_greatest = lst[1]
print("second greatest:"+second_greatest.__str__())

出:

>>> python3 test.py
Enter the 1st number:
3
Enter the 2nd number:
5
Enter the 3rd number:
12
second greatest:5

1
投票

不要使用布尔值,而是使用排序:

x=int(input("Enter the 1st number: \n"))
y=int(input("Enter the 2nd number: \n"))
z=int(input("Enter the 3rd number: \n"))
print(f'Second largest number is {sorted([x,y,z])[-2]}')

这适用于尽可能多的输入


1
投票

如果你坚持使用条件:

if x <= y <= z or z <= y <= x:
    print(y)
elif y <= x <= z or z <= x <= y:
    print(x)
else:
    print(z)

该代码基于 3 个值(xyz、xzy、...)的 6 种可能的排序。

但是,它仅适用于 3 个值并且不可扩展。


1
投票

您可以使用相同的方法,但有点复杂。你可以试试这个代码:

x = int(input("Enter the 1st number: \n"))
y = int(input("Enter the 2nd number: \n"))
z = int(input("Enter the 3rd number: \n"))


if x < y and x > z or x < z and x > y:
    print("this is second largest number", str(x))


if y < x and y > z or y < z and y > x:
    print("this is second largest number", str(y))


if z < x and z > y or z < y and z > y:
    print("this is second largest number", str(z))

我个人不喜欢这个,因为它太长了,它只适用于这 3 个变量,并且不适用于长数字。但对于初学者来说应该足够了。


0
投票
x, y, z = input().split()
x = int(x)
y = int(y)
z = int(z)

if (x-y) * (x-z) < 0:
    print(x)
elif (y-x) * (y-z) < 0:
    print(y)
else:
    print(z)
© www.soinside.com 2019 - 2024. All rights reserved.