习题5.1-5.6.md

#列表
inventory = ["sword","armor","shield","heading potion"]
print("Your items:")
for item in inventory:
    print(item)
#列表长度
print(len(inventory))
#in操作
if "sword"in inventory:
    print("Okay!")
#索引
print(inventory[1])
#切片
print(inventory[0:2])
#列表链接
chest = ["lzq","lfp"]
inventory += chest
print(inventory)
#列表可变性
inventory[5] = "dqq"
inventory[0:2] = ["future"]  #len减一
del inventory[0]  #len减一
del inventory[0:2] #len减二
print(inventory)


Your items:
sword
armor
shield
heading potion
4
Okay!
armor
['sword', 'armor']
['sword', 'armor', 'shield', 'heading potion', 'lzq', 'lfp']
['lzq', 'dqq']
#列表方法
scores = [1,2,3,4,5,6,7,8]

choice = None

while 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 = None
while 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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/DMU_lzq1996/article/details/82953005