In a modify task you take code that already works and adapt it so that it does something else.
The editor starts you off with one line: a list of four video games.
video_games = ["Mario", "Sonic", "Joust", "Zelda"]
Adapt the code so that it does all of the following, in this order.
Minecraft to the start of the list. Do not retype the list: add the
item to the list that is already there. After this step the list holds five
games, with Minecraft at index 0.print, exactly as
written:
Enter a number between 0 and 4:
Read the answer with a bare input() and cast it with int().
print, exactly as
written:
Enter the name of a video game:
Read the answer with a bare input().
Both questions are printed exactly as written above, followed by three more lines.
The first of those three is the item at the position the user asked for, printed on its own, as in:
Minecraft
The second says what happened to the game the user named. NAME stands
for exactly what the user typed:
NAME has been removed from the list
NAME has been added to the list
The third is the whole list, printed in one go, which Python shows with its square brackets, quotes and commas:
['Minecraft', 'Mario', 'Joust', 'Zelda']
The name of a game is matched exactly as it is typed, capital letters included,
so sonic is not the same game as Sonic. A game typed any other way is one
that is not in the list, so it gets added to the end.
Input:
0
Sonic
Output:
Enter a number between 0 and 4:
Minecraft
Enter the name of a video game:
Sonic has been removed from the list
['Minecraft', 'Mario', 'Joust', 'Zelda']
Input:
4
Fortnite
Output:
Enter a number between 0 and 4:
Zelda
Enter the name of a video game:
Fortnite has been added to the list
['Minecraft', 'Mario', 'Sonic', 'Joust', 'Zelda', 'Fortnite']
Input:
2
Minecraft
Output:
Enter a number between 0 and 4:
Sonic
Enter the name of a video game:
Minecraft has been removed from the list
['Mario', 'Sonic', 'Joust', 'Zelda']
(the game the user names can be the one that was added in step 1)
Input:
3
sonic
Output:
Enter a number between 0 and 4:
Joust
Enter the name of a video game:
sonic has been added to the list
['Minecraft', 'Mario', 'Sonic', 'Joust', 'Zelda', 'sonic']
(sonic is not Sonic, so it is not in the list and gets added)
Both questions are part of the output that is checked here, so their spelling,
capital letters and punctuation count. Ask each one with its own print,
exactly Enter a number between 0 and 4: and
Enter the name of a video game:, and read the answer with a bare input()
on the next line, rather than passing the question to input(...).
| Operation | What it does | How to code it |
|---|---|---|
| Output item | Outputs a single item from the list. | print(list_name[item_index])print(sweets[3]) |
| Edit item | Changes or replaces an item in the list. | list_name[item_index] = new datasweets[1] = "Haribo" |
| Add an item | Puts a new item onto the end of the list. | list_name.append(new data)sweets.append("Galaxy") |
| Remove an item | Removes an item from the list, either by its index or by the data itself. | list_name.pop(item_index)
sweets.pop(2)
list_name.pop()
list_name.remove(item)
sweets.remove("Haribo") |
| Output all items | Outputs every item in the list, one by one, using a loop. | for i in range(0, len(list_name)):
print(list_name[i])
for i in range(0, len(sweets)):
print(sweets[i])
Python also has a built in shortcut that prints the whole list in one line: print(sweets) |
Make sure that you check for the following things:
Minecraft goes at the start of the list, so it needs insert with an
index of 0, not append.int() before it is used as an
index.