# High Scores 2.0
# Demonstrates nested sequences
scores = []
choice = None
while choice != "0":
print(
"""
High Scores 2.0
0 - Quit
1 - List Scores
2 - Add a Score
"""
)
choice = input("Choice: ")
print()
# exit
if choice == "0":
print("Good-bye.")
# display high-score table
elif choice == "1":
print("High Scores\n")
print("NAME\tSCORE")
for entry in scores:
score, name = entry
print(name, "\t", score)
# add a score
elif choice == "2":
name = input("What is the player's name?: ")
score = int(input("What score did the player get?: "))
entry = (score, name)
scores.append(entry)
scores.sort(reverse=True)
scores = scores[:5] # keep only top 5 scores
# some unknown choice
else:
print("Sorry, but", choice, "isn't a valid choice.")
input("\n\nPress the enter key to exit.")
请帮助我,我真的不明白为什么这条线在这里:
score, name = entry
在下面的代码中,您可以看到一个解释所有内容的代码片段:
entry
- 这是一个元组,在代码的一个地方,它是从两个变量“组装”起来的,而在让你感到困惑的地方 - 相反,它的元素被“解包”成两个单独的变量。Python 的语法可以很容易地用一个看似普通的赋值运算符将完全不同的结构“解包”成单独的变量。