#列表方法
scores =[1,2,3,4,5,6,7,8]
choice =Nonewhile choice !="0":print("""
High Scores
0-Exit
1-Show Scores
2-Add a Score
3-Remove a Score
4-Sort Scores
""")
choice =input("Choice:")if choice =="0":print("Bye!")elif choice =="1":print("High Scores")for score in scores:print(score)elif choice =="2":
score =int(input("What score did you add?"))
scores.append(score)elif choice =="3":
score =int(input("Remove which score?"))if score in scores:
scores.remove(score)else:print(score,"isn't in the scores list.")elif choice =="4":
scores.sort(reverse=True)else:print("Sorry,%d is not a valid chice.")
High Scores
0-Exit
1-Show Scores
2-Add a Score
3-Remove a Score
4-Sort Scores
Choice:1
High Scores
1
2
3
4
5
6
7
8
High Scores
0-Exit
1-Show Scores
2-Add a Score
3-Remove a Score
4-Sort Scores
Choice:2
What score did you add?10
High Scores
0-Exit
1-Show Scores
2-Add a Score
3-Remove a Score
4-Sort Scores
Choice:3
Remove which score?5
High Scores
0-Exit
1-Show Scores
2-Add a Score
3-Remove a Score
4-Sort Scores
Choice:4
High Scores
0-Exit
1-Show Scores
2-Add a Score
3-Remove a Score
4-Sort Scores
Choice:1
High Scores
10
8
7
6
4
3
2
1
High Scores
0-Exit
1-Show Scores
2-Add a Score
3-Remove a Score
4-Sort Scores
Choice:0
Bye!
#嵌套结构
scores =[]
choice =Nonewhile choice !=0:print("""
High Scores 2.0
0-Exit
1-Show Scores
2-Add a Score
""")
choice =input("Choice:")if choice =="0":print("Bye!")elif choice =="1":print("High Scores\n")for entry in scores:
score,name = entry
print(name,"\t",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]else:print("Sorry,",choice,"is not valid choice.")
High Scores 2.0
0-Exit
1-Show Scores
2-Add a Score
Choice:2
What is the player's name?lzq
What score did the player get?97
High Scores 2.0
0-Exit
1-Show Scores
2-Add a Score
Choice:2
What is the player's name?dqq
What score did the player get?99
High Scores 2.0
0-Exit
1-Show Scores
2-Add a Score
Choice:1
High Scores
dqq 99
lzq 97
High Scores 2.0
0-Exit
1-Show Scores
2-Add a Score
Choice:0
Bye!
High Scores 2.0
0-Exit
1-Show Scores
2-Add a Score