You are on page 1of 3

Lab Practice 6: Objects, Lists and

Sequences
BANA3020 Introduction to Programming with Python Fall 2023 – Week 07

Lab Practice Submission Instructions:


• This is an individual lab practice and will typically be assigned in the laboratory (computer lab). You can
use your personal computer but all quizzes and practical exams will be performed with a lab computer.
• Your program should work correctly on all inputs. If there are any specifications about how the program
should be written (or how the output should appear), those specifications should be followed.
• Your code and functions/modules should be appropriately commented. However, try to avoid making
your code overly busy (e.g., include a comment on every line).
• Variables and functions should have meaningful names, and code should be organized into function-
s/methods where appropriate.
• Academic honesty is required in all work you submit to be graded. You should NOT copy or share your
code with other students to avoid plagiarism issues.
• Use the template provided to prepare your solutions.
• You should upload your .py file(s) to Canvas and CMS platform before the end of the day.
• Submit separate .py file for each Lab problem with the following naming format: Lab3_Q1.py. Note: If
you are working on Jupyter Notebook, you need to download/convert it to Python .py file for submission.
• Late submission of lab practice without an approved extension will incur the following penalties:

(a) No submission by the deadline will incur 0.25 point deduction for each problem (most of the
problems are due at the end of the lab session).

(b) The instructor will deduct an additional 0.25 point per problem for each day past the deadline.

(c) The penalty will be deducted until the maximum possible score for the lab practice reaches zero
(0%) unless otherwise specified by the instructor.

Lab Practice 6 Page 1


Problem 1
Write a program that input a string and ask user to delete the first occurrence of a given word from a string.
If the word doesn’t exist, print the original string unchanged.
It is best if you can perform this exercise using list methods, i.e remove(), instead of using a loop. You might
need to apply your knowledge of try-except to handle special cases.

Submit your code to CMS.

Sample Input and Expected Output (Test Cases)


1 >>> python Lab6_Q1 . py
2 He is a good boy .
3 good
4 He is a boy .

Problem 2
You’re going shopping for a friend, and you need to buy 3 items. First, create an empty list for your shopping
cart called my_cart = []. Then:

• Ask for 3 "items" to buy. Inputs can be any string ("egg","milk","sausage","shoes","iphone",


etc.)

• Add the 3 "items" to the cart.

Unfortunately your friend is lactose intolerant! That means your friend DO NOT want you to buy ANY of
the following items:
dairy_products = ["cheese", "milk", "cream", "butter", "chocolate", "yogurt"]

• Remove any items in dairy_products from my_cart (recall the in keyword from lecture). It should
be a list of strings without any dairy products.

Hint: There are multiple ways to solve this problem. You can check for each input and only add to cart if
the input is not in dairy_products, or you can add everything to the list, and remove the dairy products
afterward.

• Print my_cart as a comma-separated string (use join). If there aren’t any non-dairy item, print an
empty string.

Submit your code to CMS.

Sample Input and Expected Output (Test Cases)


1 >>> python Lab6_Q2 . py
2 egg
3 bread
4 bacon
5 egg , bread , bacon

Lab Practice 6 Page 2


1 >>> python Lab6_Q2 . py
2 cheese
3 beef
4 butter
5 beef

1 >>> python Lab6_Q2 . py


2 cheese
3 milk
4 chocolate
5

1 >>> python Lab6_Q2 . py


2 milk
3 bottle
4 carrots
5 bottle , carrots

Problem 3
Write a Python program that prompts the user to input a list of names separated by commas. Your task is to
extract and display the first, middle, and last names in the list.

Utilize list methods and indexing to achieve this without using loops. Note: To find the middle element in
a list, use length//2 for both odd and even lists. This convention helps in accessing the center element
without loops.

Submit your code to CMS.

Sample Input and Expected Output (Test Cases)


1 >>> python Lab6_Q3 . py
2 John , Emma , Liam , Olivia , Noah
3 John
4 Liam
5 Noah

Lab Practice 6 Page 3

You might also like