CAP Term 2 Tutorial 4 – Python: After Class Activities
Activity 1: Predicting the output
Create the following code. Before you run the code, complete all the comments (6
red and 1 green), then check your comments against the output to see if you
were correct. Save as lists_output.py
meals = ['breakfast', 'brunch', 'lunch', 'afternoon snack', 'dinner']
#The length is
print ('The length is', len(meals))
[Link]('hot Chocolate')
#The length is now
print ('The length is now', len(meals))
print ('meals is', meals)
#The index of lunch is
print('Lunch has an index of',[Link]('lunch'))
print(meals)
#The first element after sort is
[Link]()
print('meals sorted is', meals)
#The deleted element is
del meals[1]
print('index 1 deleted leaves', meals)
#The deleted element is
[Link](1)
print('index 1 deleted leaves', meals)
'''
0 is
1 is
2 is
3 is
'''
for i in range(len(meals)):
print(i, 'is', meals[i])
if 'brunch' not in meals:
print('brunch is gone')
else:
print('brunch is breakfast and lunch')
1
Activity 2: days_of_week
Create a new Python program named days_of_week.py that repeatedly asks the
user for the days of the week. Include a function that checks if the user's answer
is a day of the week and that it is not already entered by the user. Give the user 7
attempts and then tell them how many they got right.
Save as days_of_week.py
The code will:
• Ask the user for a day of the week.
• Add the day to the user's list if the function returns True (or yes).
• Stop after 7 questions
The function will
• Check if the answer is already entered, and tell the user
• Check if the answer is not a day of the week, and tell the user.
• Return True (or Yes) if the answer is a day of the week and is not already entered.
Possible Output:
Enter a day of the week: Monday
Sunday has been added to our days of the week.
['Monday']
Enter a day of the week: Tuesday
Monday has been added to our days of the week.
['Monday', 'Tuesday']
Enter a day of the week: Thursday
Tuesday has been added to our days of the week.
['Monday', 'Tuesday', 'Thursday']
Enter a day of the week: Friday
Friday has been added to our days of the week.
['Monday', 'Tuesday', 'Thursday', 'Friday']
Enter a day of the week: Friday
Sorry Friday is already in our list.
Enter a day of the week: March
March is not a day of the week.
Enter a day of the week: Wenday
Wenday is not a day of the week.
You have correctly entered 4 days of the week.
['Monday', 'Tuesday', 'Thursday', 'Friday']
Well done! You have populated the list.
2
Activity 3 w3schools online List exercise
Click the link below to five exercises (Exercises 1-5) to reinforce the skills learnt in Tutorial
4. For exercise 4 you will need to refer to the w3schools Lists tutorial
w3schools List exercises (1-5)