我寫了這為我的初級班,我不知道如何獲得NAME的的第二個學生與第二最高得分。經過測驗,我相信該代碼適用于第一個學生。
我想我需要的是將最高分存盤到變數 high_score 中,然后將下一個最高分存盤到 second_score 中,然后將第三個分數與最高和第二個分數進行比較。但是我對如何獲得得分第二高的學生的名字感到困惑。
num_students = int(input("enter number of students: "))
high_score = 0
high_name = ""
second_name = ""
second_score = 0
for i in range(1,num_students 1):
if num_students < 2:
break
if num_students >= 2:
name = input("enter student name: ")
score = int(input("enter student score: "))
if score > second_score:
if score > high_score:
second_score = high_score
high_score = score
high_name = name
elif score < high_score:
second_score = score
second_name = name
print()
print("Top Two Students")
print()
print(high_name,"'s score is", high_score)
print(second_name,"'s score is", second_score)
uj5u.com熱心網友回復:
除了 Gary02127 的解決方案,我想指出其他一些改進:
你應該移出
if num_students < 2你的回圈。在用戶輸入學生人數后檢查一次條件就足夠了。你也可以寫
for i in range(num_students). 范圍是否以開頭0或1因為您沒有使用i.此外,如果您不使用變數,則可以使用
_(throwaway variable) 代替。for _ in range(num_students)在此處查看更多資訊:Python 中單個下劃線“_”變數的用途是什么?.
代替:
if score > second_score:
if score > high_score:
# ...
else:
# ...
你也可以這樣寫:
if high_score < score:
# ...
elif second_score < score:
# ...
這是考慮到建議改進的詳細解決方案:
num_students = int(input("Enter number of students: "))
if num_students < 2:
exit()
highest_name = None
highest_grade = 0
second_highest_name = None
second_highest_grade = 0
for _ in range(num_students):
name = input("Enter student's name: ")
grade = int(input("Enter student's score: "))
if highest_grade < grade:
second_highest_name = highest_name
second_highest_grade = highest_grade
highest_name = name
highest_grade = grade
elif second_highest_grade < grade:
second_highest_name = name
second_highest_grade = grade
print(highest_name, highest_grade) # highest grade
print(second_highest_name, second_highest_grade) # second highest grade
您還可以使用listand sorted()(內置函式):
from operator import itemgetter
num_students = int(input("Enter number of students: "))
if num_students < 2:
exit()
grades = []
for _ in range(num_students):
name = input("Enter student's name: ")
grade = int(input("Enter student's score: "))
grades.append((name, grade))
grades = sorted(grades, key=itemgetter(1), reverse=True)
print(grades[0]) # highest grade
print(grades[1]) # second highest grade
您還可以使用專門的串列,例如SortedList(需要外部包):
from sortedcontainers import SortedList
num_students = int(input("Enter number of students: "))
if num_students < 2:
exit()
grades = SortedList(key=lambda record: -record[1])
for _ in range(num_students):
name = input("Enter student's name: ")
grade = int(input("Enter student's score: "))
grades.add((name, grade))
print(grades[0]) # highest grade
print(grades[1]) # second highest grade
筆記:
sorted()和之間的區別str.sort()。sorted()創建一個新的 sortedlist而str.sort()修改當前list.itemgetter(1)等于lambda record: record[1]。- 你可以安裝排序串列:
pip install sortedcontainers。
uj5u.com熱心網友回復:
這是基于我之前對您原始帖子的評論的解決方案。
num_students = int(input("enter number of students: "))
high_score = 0
high_name = ""
second_name = ""
second_score = 0
for i in range(1,num_students 1):
if num_students < 2:
break
# if num_students >= 2: # don't need this "if" after previous "break"
name = input("enter student name: ")
score = int(input("enter student score: "))
if score > second_score:
if score > high_score:
second_score = high_score
second_name = high_name # NEW
high_score = score
high_name = name
else: # simplified to just a plain "else:"
second_score = score
second_name = name
print()
print("Top Two Students")
print()
print(high_name,"'s score is", high_score)
print(second_name,"'s score is", second_score)
請注意,將最后一個 "elif ...:" 簡化為簡單的 "else:" 還解決了一個簡單的錯誤,即再次輸入當前高分可能會被忽略而不被捕獲。如果您按原樣運行代碼并使用輸入值“100 80 100”,您會發現第二個高分設定為 80 而不是 100。
快樂編碼!
uj5u.com熱心網友回復:
您可以簡單地將詳細資訊存盤在串列中。并用于list.sort()根據分數對值進行排序。
這是作業代碼:
num_students = int(input("Enter number of students: "))
scores = []
if num_students < 2:
print('Number of students less than 2')
exit()
for i in range(num_students):
name = input("Enter student's name: ")
score = int(input("Enter student's score: "))
scores.append([name, score])
scores.sort(key=lambda details: details[1], reverse=True)
print("Top Two Students")
print()
print(f"{scores[0][0]}'s score is {scores[0][1]}")
print(f"{scores[1][0]}'s score is {scores[1][1]}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322603.html
上一篇:如何在回圈中反轉字串
