Python Lists Review
lastname_inventory.py and copy it to your OneDrive
folder. It is due at the end of class today.
For each question below, think about the answer before clicking "Reveal Answer". If you get it wrong, make sure you understand why before moving on.
Write one line of code to create a list called knights that
contains the names "Arthur", "Lancelot", and
"Galahad".
knights = ["Arthur", "Lancelot", "Galahad"]
Given knights = ["Arthur", "Lancelot", "Galahad"], what
does print(knights[0]) print?
Arthur
Given knights = ["Arthur", "Lancelot", "Galahad"], what
does print(knights[-1]) print?
Galahad — Using -1 always gives you the last
item in the list.
Given knights = ["Arthur", "Lancelot", "Galahad"], what
does print(len(knights)) print?
3
Given knights = ["Arthur", "Lancelot", "Galahad"], write
one line of code to add "Percival" to the end of the list.
knights.append("Percival")
Given knights = ["Arthur", "Lancelot", "Galahad"], write
one line of code to replace "Lancelot" (at index 1) with
"Gawain".
knights[1] = "Gawain"
Given knights = ["Arthur", "Lancelot", "Galahad"], write
one line of code to remove "Lancelot" from the list.
knights.remove("Lancelot")What does knights.pop() do (with no argument)?
Given scores = [95, 82, 74, 100, 68], write a
for loop that prints each score on its own line.
for score in scores:
print(score)
Given inventory = ["sword", "shield", "potion"], write an
if statement that prints
"You have a potion!" if "potion" is in the
list.
if "potion" in inventory:
print("You have a potion!")
Given scores = [95, 82, 74, 100, 68], write code to print
the total number of scores in the list.
print(len(scores))
Given names = ["Charlie", "Alice", "Bob"], write one line
of code to sort the list alphabetically.
names.sort() — After this, names will be
["Alice", "Bob", "Charlie"].
If you have finished the Inventory Manager Project and have finished the review questions, try this challenge for additional practice.
Write a program called High Score Tracker that lets a user manage a leaderboard for their favorite game. The program should:
1 — Show the leaderboard (names and scores)2 — Add a new player and score3 — Show the highest score4 — Show the number of players5 — QuitHere is an example of how the program might look:
== HIGH SCORE TRACKER == 1. Show leaderboard 2. Add player 3. Show highest score 4. Show number of players 5. Quit Choose an option: 1 Leaderboard: Marcus - 4500 Jake - 3200 Deon - 5100 Choose an option: 3 Highest score: 5100 Choose an option: 2 Player name: Tyler Score: 4800 Tyler added! Choose an option: 5 Goodbye!
Hint: Use the built-in max() function to
find the highest score in your scores list — for example,
max(scores).
Save your program as lastname_highscores.py and copy it to your OneDrive folder if you finish it.