python中的数组,数组不会排列,(编码初学者)



我试图用python编写一些数组,但当我运行下面的代码时;排序";以及";人;名单排不好队,也就是说,这类人找错人了。我是python的新手,所以请保持代码简单,谢谢。我知道很多代码都是重复的,但我正在努力。问题主要是最后3行,但我已经附上了其余的,只是为了确定。

people = []
score = []
people.append("Doc")
people.append("Sarah")
people.append("Jar-Jar")
Doc1 = int(input("Enter the test score for Doc "))
print("Test score:", Doc1)
Sarah1 = int(input("Enter the test score for Sarah "))
print("Test score:", Sarah1)
Jar1 = int(input("Enter the test score for Jar-Jar "))
print("Test score:", Jar1)
score.append(Doc1)
score.append(Sarah1)
score.append(Jar1)
sort = sorted(score, reverse = True)
print("[",sort[0],", '", people[0],"']")
print("[",sort[1],", '", people[1],"']")
print("[",sort[2],", '", people[2],"']")

将分数和名称组成元组,以便将它们放在一起,并将它们放入列表中;然后对列表进行排序。

people = ["Doc", "Sarah", "Jar-Jar"]
scores = [
(int(input(f"Enter the test score for {person} ")), person)
for person in people
]
scores.sort(reverse=True)
for score, person in scores:
print(f"[{score}], '{person}'")
Enter the test score for Doc 1
Enter the test score for Sarah 4
Enter the test score for Jar-Jar 3
[4], 'Sarah'
[3], 'Jar-Jar'
[1], 'Doc'

最新更新