python 找出第二大值


listNum = []

num = 0
while num < 5:
val = int(input())
listNum.append(val)
num += 1
print(listNum)

'''
#升序排序
listNum.sort()

count = listNum.count(listNum[len(listNum) - 1])
c = 0
while c < count:
listNum.pop()
c += 1

print(listNum[len(listNum) - 1])
'''

if listNum[0] >= listNum[1]:
max = listNum[0]
sec = listNum[1]
else:
max = listNum[1]
sec = listNum[0]

index = 2
while index < len(listNum):
if listNum[index] >= sec:
sec = listNum[index]
if listNum[index] >= max:
sec = max
max = listNum[index]
index += 1

print(sec)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM