嘿我最近開始學習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")
uj5u.com熱心網友回復:
通過將輸入添加到串列并對其進行排序,您可以輕松獲得第二大數字。
試試這個:
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")
uj5u.com熱心網友回復:
使用minand max,它們是 python 的內置函式
x = 12
y = 5
z = 3
second_max = min(x, max(y, z))
print(second_max)
出去
5
無論值如何,您總是會獲得第二個最大值。
uj5u.com熱心網友回復:
你可以這樣做:
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
uj5u.com熱心網友回復:
不要使用布林值,而是使用排序:
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]}')
這適用于盡可能多的輸入
uj5u.com熱心網友回復:
如果您堅持使用條件:
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 個值并且不可擴展。
uj5u.com熱心網友回復:
您可以使用相同的方法,但有點復雜。你可以試試這段代碼:
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 個變數,并且不適用于長數字。但是對于初學者來說應該足夠了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408134.html
標籤:
