0% found this document useful (0 votes)
26 views4 pages

A2 Worksheet - Append and Remove Items

Uploaded by

Viet Dang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

A2 Worksheet - Append and Remove Items

Uploaded by

Viet Dang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

KS4 - Programming Activity sheet

Lesson 29 - Arrays and lists

Append and remove items


Code snippets .
Append an item
shopping = []

shopping.append("Bread")

Remove an item
shopping = ["bread", "cheese", "milk"]

shopping.remove("bread")

Append an item using a variable


item = "pizza"
shopping.append(item)

Task .
Step 1

Copy the start code below into your development environment.

1 shopping = ["bread", "cheese", "milk"]

Step 2

Use the append operator to add two new items to the shopping list:
● Eggs
● Flour

Step 3

Use the remove operator to remove cheese from the shopping list.

Page 1 Last updated: 31-05-2024


KS4 - Programming Activity sheet
Lesson 29 - Arrays and lists

Step 4

Print your list to check that your code has worked. The output should look like
this:

['bread', 'milk', 'eggs', 'flour']

A shopping list program .


Use the two new operators append and remove to create a shopping list program.
Your program should:

● Continue to ask for new items until the user has finished
● Prompt if an item should be removed or added
● If the choice is to remove the item, it should remove it
● If the choice is to add the item, it should append it
● Display the finished list at the end of the program

Here is some example input and output to help you with your design:

Example
Note: Given the input you see in this sample interaction, this is the output your program
should produce.

The user is given a prompt Would you like to edit your shopping list? Y/N

The user enters their response Y

The user is given a prompt Would you like to add or remove an item? A/R

The user enters their response A

The user is given a prompt Enter an item to add:

The user enters their response Eggs

The user is given a prompt Would you like to edit your shopping list? Y/N

The user enters their response Y

The user is given a prompt Would you like to add or remove an item? A/R

The user enters their response A

Page 2 Last updated: 21-05-21


KS4 - Programming Activity sheet
Lesson 29 - Arrays and lists

The user is given a prompt Enter an item to add:

The user enters their response Milk

The user is given a prompt Would you like to edit your shopping list? Y/N

The user enters their response Y

The user is given a prompt Would you like to add or remove an item? A/R

The user enters their response R

The user is given a prompt Enter an item to remove:

The user enters their response Milk

The user is given a prompt Would you like to edit your shopping list? Y/N

The user enters their response N

The user is shown the list of ['eggs']


items
# Continue to ask for new items until the user has finished
# Prompt if an item should be removed or added
# If the choice is to remove the item, it should remove it
# If the choice is to add the item, it should append it
# Display the finished list at the end of the program
shopping = ["bread", "cheese", "milk"]
shopping.append("eggs")
shopping.append("flour")
shopping.remove("cheese")
print(shopping)
idea = "yes"
while idea == "yes":
opinion = input("Would you like to change your shopping list? (yes/no)\
n")
idea = opinion.lower().strip('')
if idea == "no":
print("Alright, here's your shopping list:", shopping)
break
elif idea != "yes" and idea != "no":
print("sorry I don't understand your command")
break
choice = input("Would you like to add/remove an item?")
think = choice.lower().strip("")
if think == "add":
add = shopping.append(str(input("Enter an item to your list")))
elif think == "remove":

Page 3 Last updated: 21-05-21


KS4 - Programming Activity sheet
Lesson 29 - Arrays and lists

remove = shopping.remove(str(input("Enter an item to your list")))


else:
print("sorry I don't understand your command")
break

Explorer task .
Revisit your ‘Simon says…’ program and add extra functionality to allow the
player to enter the movements for the game before they play.

Resources are updated regularly — the latest version is available at: ncce.io/tcc.

This resource is licensed under the Open Government Licence, version 3. For more information on this
licence, see ncce.io/ogl.

Page 4 Last updated: 21-05-21

You might also like