According to Weimer

Computer Skills Challenge -- under construction

Introduction

Many students that I have don't have much experience with file management when they enroll in my classes. This exercise lets me know what skills the students are coming to me with. I encourage the students to help each other as they work through it. When everyone has finished, we spend time going over the different ways they accomplished each task and why they did it that way.

If you are not using Windows, you will need to modify the script.

Time to Complete

Middle schoolers usually takes one to two 45 min class periods if it is the student's first time doing the activity. High Schoolers typically take about half to two-thirds the time it takes the middle schoolers.

Files

Python File Download Questions Download

Questions Discussion

Once step 5 is complete

Some common errors I see students make are listed below.

  • Not completing step 1, leading to double extensions on the filenames.
  • Deleting the original images once they created the ZIP file.
  • Capitalization is not correct.
  • Extra spaces after the words in the text file. Whitespace counts.

Code Discussion

To be done on a Windows machine.

import os
import glob
from PIL import Image
from zipfile import ZipFile
import winsound

correct_structure = ['Audio\\game_audio.wav', 'Docs\\foods.txt',
                'Docs\\my_text_file.txt', 'Images\\image1.jpg',
                'Images\\image2.jpg', 'Images\\image3.jpg',
                'Images\\my_images.zip']
zip_correct = ['image1.jpg', 'image2.jpg', 'image3.jpg']

print("""

    Welcome!
    Let's get started...

""")

filepaths = glob.glob('**/*.*', recursive=True)
#print(filepaths)
print(" ")
filepaths.remove(filepaths[0])
if "Images\\Thumbs.db" in filepaths:
    filepaths.remove('Images\\Thumbs.db')
print("  Checking files and folders....")

while correct_structure != filepaths:    
    print("  It looks like you have a problem with your files.")
    input("  Find the error and press 'enter' when ready to try again.")
    print(" ")
    print("  Checking files and folders....")
    filepaths = glob.glob('**/*.*', recursive=True)
    filepaths.remove(filepaths[0])
    if "Images\\Thumbs.db" in filepaths:
        filepaths.remove('Images\\Thumbs.db')
        
        
print("  It looks like everything is where it should be!")
print(" ")
    
print("""  Next I will ask you some questions
    about the images you added...""")
image1_size = os.stat('Images\\image1.jpg').st_size
image2_size = os.stat('Images\\image2.jpg').st_size
image3_size = os.stat('Images\\image3.jpg').st_size
total_size = image1_size + image2_size + image3_size

size_response = 0
guesses = 0
while int(size_response) != total_size:
    guesses = guesses + 1
    if guesses > 4:
        print("  Make sure you are not just guessing.")
    size_response = input("  What is the total size(in bytes) of all three images? ")
    size_response = size_response.replace(",", "")
    size_response = size_response.replace(".", "")
    while not size_response.isdigit():
        print("""    Your answer should be an integer.

""")
        size_response = input("  What is the total size(in bytes) of all three images? ")
        size_response = size_response.replace(",", "")
        size_response = size_response.replace(".", "")
    if int(size_response) == total_size:
        print("  That is the correct total!")
        input("  Press 'enter' to continue.")
        print(" ")
    elif int(size_response) > total_size:
        print("  That is too high!")
        print(" ")
    else:
        print("  That is too low!")
        print(" ")

img = Image.open('Images\\image2.jpg')
width = img.size[0]
width_response = 0
guesses = 0
while int(width_response) != width:
    guesses = guesses + 1
    if guesses > 3:
        print("  Make sure you are not just guessing.")
        print(" ")
    width_response = input("  What is the width of image2.jpg? ")
    width_response = width_response.replace(",", "")
    while not width_response.isdigit():
        print("""    Your answer should be an integer.
    Do not include decimal points or commas in your answer.

""")
        width_response = input("  What is the width of image2.jpg? ")
        width_response = width_response.replace(",", "")
    if int(width_response) == width:
        print("  That is the correct total!")
        print("  Great job!")
        input("  Press 'enter' to continue.")
        print(" ")
    elif int(width_response) > width:
        print("  That is too high!")
        print(" ")
    else:
        print("  That is too low!")
        print(" ")
img.close()

with open('Docs\\foods.txt') as f:
    lines = f.read().splitlines()

print("  Checking the contents of foods.txt... ")

while lines != ['apple', 'taco', 'pear']:
    print("  The contents of foods.txt are not correct.")
    input("  Make the necessary changes and press 'enter' to continue.")
    print(" ")
    print("  Checking the contents of foods.txt... ")
    with open('Docs\\foods.txt') as f:
        lines = f.read().splitlines()
        
print("  It looks like foods.txt is correct!")
input("  Press 'enter' to continue.")
print(" ")    
    
file_name = 'Images\\my_images.zip'

with ZipFile(file_name, 'r') as zip:    
    zip_contents =  zip.namelist()            

print("  Checking the contents of my_images.zip... ")

while zip_contents != zip_correct:
    print("  The contents of my_images.zip are not correct.")
    input("  Make the necessary changes and press 'enter' to continue.")
    print(" ")
    print("  Checking the contents of my_images.zip... ")
    with ZipFile(file_name, 'r') as zip:    
        zip_contents =  zip.namelist()
        
print("  It looks like my_images.zip is correct!")
print(" ")
print("""

    ░█████╗░░█████╗░███╗░░██╗░██████╗░██████╗░░█████╗░████████╗░██████╗██╗
    ██╔══██╗██╔══██╗████╗░██║██╔════╝░██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██║
    ██║░░╚═╝██║░░██║██╔██╗██║██║░░██╗░██████╔╝███████║░░░██║░░░╚█████╗░██║
    ██║░░██╗██║░░██║██║╚████║██║░░╚██╗██╔══██╗██╔══██║░░░██║░░░░╚═══██╗╚═╝
    ╚█████╔╝╚█████╔╝██║░╚███║╚██████╔╝██║░░██║██║░░██║░░░██║░░░██████╔╝██╗
    ░╚════╝░░╚════╝░╚═╝░░╚══╝░╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░╚═════╝░╚═╝

""")
winsound.PlaySound('Audio\\game_audio.wav', winsound.SND_FILENAME)
input("""  Press 'enter' to close the program  """)  
January 8, 2023 Keywords: Parsons problems