Example code - exam form

From 22116
Revision as of 10:45, 3 September 2026 by WikiSysop (talk | contribs) (Created page with "At this and the next lesson plus the exam, you will have to do the exercises differently. This is both an example and an explanation.<br> How you will achieve it using your tools - VScode, Jupyter Notebook, text editor, WSL2, command line, etc - is your problem to solve. You get a number of assignments or exercises that you have to solve. In this example it is demonstrated with a single task. Make a function that accepts two parameters; an input file name and and outpu...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

At this and the next lesson plus the exam, you will have to do the exercises differently. This is both an example and an explanation.
How you will achieve it using your tools - VScode, Jupyter Notebook, text editor, WSL2, command line, etc - is your problem to solve.

You get a number of assignments or exercises that you have to solve. In this example it is demonstrated with a single task.

Make a function that accepts two parameters; an input file name and and output filename. The input file contains one value per line, but the value might be anything, string, integer, float, positive or negative. The function must read the input file, discard any value that is not positive, and output the log of the non-discarded numbers to the output file.

You will as a template for your hand-in get a file that looks like this:

#!/usr/bin/env python3
# Above is the shebang line - which tells which program should interpret the rest of the file

# The libraries necessary for the exercises - use no other
import os, sys, re

####################################################################################
# You must fill the functions with code that does the job mentioned.               #
# All the functions must work together coherently for the exercise to be complete. #
# The main exercise function should call the other functions in it's group.        #
# You can add your own functions, but you still need to make the ones described.   #
# Any personal functions should follow the naming pattern demonstrated.            #
# You are expected the delete the "pass" statements as they are present only for   #
# syntax reasons. The template won't run without them.                             #
####################################################################################

################################
### FUNCTIONS FOR EXERCISE 1 ###
################################

def exercise_1(filenamein:str, filenameout:str) -> None:
    # Read file, discards line with non-positive numbers, output the log
    pass


############################
# Main program starts here #
# Not necessary to change  #
############################

# Below if statement allows you to execute the main code when the file runs as a script,
# but not when it is imported as a module.
if __name__ == "__main__":

    # First exercise
    exercise_1('values.txt', 'log_values.txt')