Stock Level Checker Program – Python Challenge

With this Python challenge, we’re going to focus on using file handling techniques to create a simple yet practical program to be used by a shop assistant to quickly check their stock levels. The goal is to write a Python script that allows users to check the availability of clothing items in stock. The program will prompt the user to select a piece of clothing, a size, and a colour, and then it will check a CSV file to see if the item is available.

CSV File?

To begin, we will need a CSV file that contains the stock information. CSV files (Comma Separated Values) are text files that store data where each record is stored on a line and each field is separated using a comma “,”. Sometimes we use other symbols instead of the comma such as a semi-colon “;” or a pipe “|”. Below is an example of what the CSV file might look like:

item,size,color,quantity
t-shirt,S,red,5
t-shirt,M,blue,3
t-shirt,L,red,2
t-shirt,M,red,0
t-shirt,S,blue,0
t-shirt,S,green,1
shorts,M,blue,1
shorts,L,green,0
shorts,S,green,2
shorts,S,blue,3
cap,L,red,4
cap,M,white,2
cap,M,black,0
cap,S,red,5

We have saved this data into a CSV file called stock.csv available as a separate tab in the Python IDE below.

The following Python code can be used to read our CSV file line by line and extract the data from each line:

file = open("stock.csv","r")

for line in file:
  data = line.split(",")
  item = data[0]  
  size = data[1]
  colour = data[2]
  stockLevel = int(data[3])
  print(item + " - " + size + " - " + colour + " - Quantity in Stock: " + str(stockLevel))
  
file.close()

Python Code

Your task is to complete the code provided below to perform the following tasks:
Your task is to create a Python program that performs the following steps:

    Prompt the user to enter a piece of clothing (e.g. t-shirt, shorts, cap).
    Ask the user to select a size (e.g. S, M, L).
    Request the user to choose a colour (e.g. red, blue, green, white, black).
    Check a CSV file to see if the item is in stock.
    Display a message indicating whether the item is available or out of stock.

Note that an item is out of stock if its stock level is zero or if it is not listed in the CSV file.

Testing

Once your code is done, complete the following tests to check that your code is working as it should:

Test # Input Values Expected Output Actual Output
#1 Item: T-Shirt
Size: L
Colour: Red
This item is in stock (Stock Level: 2)
#2 Item: Cap
Size: M
Colour: Black
This item is out of stock
#3 Item: Shorts
Size: S
Colour: Blue
This item is in stock (Stock Level: 3)
#4 Item: Cap
Size: L
Colour: White
This item is out of stock

Next step…

This challenge is a great way to practise file handling in Python. By completing this task, you’ll gain experience in reading from CSV files and processing user input. Feel free to expand the program by adding more features, such as updating the stock quantity or allowing users to add new items to the stock. You may also make your program more robust by validating user inputs and converting the case to match the case used in the CSV file. (e.g. size automatically converted to uppercase and colour automatically converted to lowercase).

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: