113k views
3 votes
Task 2: odd or even()

1) Complete the odd_or_even() function according to the function documentation on the .py source file provided.
Mark Breakdown
2) 2 additional doctests are created
a) Doctest output is accurate
b) Doctest passes
3) Does function pass all assignment doctests provided?
4) Function body code

1 Answer

6 votes

Final answer:

The student must complete and test the odd_or_even() Python function by following the provided function documentation, writing code to determine if a number is odd or even, and creating additional doctests to ensure accuracy.

Step-by-step explanation:

Understanding the odd_or_even() Function

The student is tasked with completing a Python function called odd_or_even() that determines whether a given number is odd or even. This function is part of a programming exercise, and the student needs to follow the function documentation provided to implement the function correctly. Moreover, the student must create additional doctests to validate the function's behavior. Doctests are examples embedded in a function's docstring that are used as test cases to check that the function works as expected. If the function passes all the provided assignment doctests and the additional ones created by the student, it is considered correct.

The function body code should consist of a simple conditional check to assess if the input number is divisible by 2, which would indicate it's even, or not, which would indicate it's odd. Typically, this is done using the modulo operator (%) in Python. The function would return a string such as 'even' or 'odd' depending on this condition.

To write additional doctests:

  1. Write a comment string (docstring) under the function definition.
  2. Within this string, include examples of function calls followed by '>>>' and the expected result on the next line.
  3. Ensure the doctests cover different scenarios and edge cases to validate the function comprehensively.

Once the doctests are written, they can be run using the doctest module to ensure the function code behaves as intended.

User Hellpanderr
by
8.6k points