Search
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.
example.py
foo(a, b)
What you must submit in this case:
def foo(a,b):
a
b
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:
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
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.,
if __name__ == '__main__':
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:
input()
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.