Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Solutions to homeworks, tests, etc.

Formal requirements

The homeworks, tests, or other programming tasks in this course contain a few required parts which must be fulfilled, otherwise the solution will be automatically evaluated as incorrect. The task is usually specified as:

In module example.py, create function foo(a, b) which takes 2 numeric arguments and returns its sum.

What you must submit in this case:

  • a single file which must be named exactly example.py and
    • which must contain function named and defined exactly as def foo(a,b): (the names of parameters a and b actually are not important);
  • you can submit also other files, e.g. your testing code, but these will not be automatically evaluated.

How to correctly organize the code in the file

Students often organize the code in the Python script in a wrong way which can prevent the solution from being successfully evaluated. We suggest the following two options for code organization:

Option 1

Submit Python module containing only the function definition(s), e.g.,

def foo(a, b):
    return a + b

Or, in case more than one function is required in the task specifications, or if you decided to decompose your solution into several functions, the file may contain more than 1 function definitions, e.g.:

def foo(a, b):
    """This function is stupidly decomposed"""
    return bar(a,b)
 
def bar(a, b):
    return a + b
 
def baz(a, b):
    return a*b

Option 2

Often, students want to add some testing code directly to the module. This is perfectly correct and valid desire. But you must do it with a bit of care such that the code works fine during the automatic evaluation. You can do it like this:

def foo(a, b):
    return a + b
 
if __name__ == '__main__':
    # Testing code goes here
    print(foo(0,0) == 0)
    print(foo(1,1) == 2)
    print(foo(0,100) = 100)

If you do not understand, what the condition if __name__ == '__main__': actually does, you can consult online resources, e.g.,

Do not do this!

Beginners often write programs as scripts. Very often they want to use input() function to read the inputs from the keyboard. Such code may look like this:

a = int(input('Give me the first number:'))
b = int(input('Give me the second number:'))
 
def foo(a, b):
    return a + b
 
print('The result of the addition:', foo(a,b))

Such code is not reusable and will not be evaluated correctly in the upload system!!! The evaluation script in the upload system tries to import the function from your module. But during the import, your module is executed. This means that it waits for input from the keyboard (from standard input), but none is provided, and the evaluation ends up because of timeout. (And with no points.)

Our suggestion: do not use the input() function in your homeworks at all, because the testing with it is quite tedious! In the example above, you must type 2 numbers each time you run the script. If you change the script and want to test the result for the same inputs, you have to re-type them again! It is much better to write the test cases directly into the testing code, as examplified in the previous section.

If you really want to use input(), then do it as shown in section Option 2:

def foo(a, b):
    return a + b
 
if __name__ == '__main__':
    # Testing code goes here
    a = int(input('Give me the first number:'))
    b = int(input('Give me the second number:'))
    print('The result of the addition:', foo(a,b))

This way the input() function will not be executed during the import, and will not prevent the solution from being evaluated.

Credits to the 'be5b33prg' course for this tutorial page.

courses/be5b33kui/tutorials/homeworks.txt · Last modified: 2021/02/17 10:40 by gamafili