1

I have just started learning python and I am following some online lectures from my university. I have just been introduced to writing doctests in scripts. Below is my code:

 """ Our first python source file. """

from operator import floordiv, mod 

def divide_exact(n,d):
    """ Return the quotient and remainder.

    >>> q, r = divide_exact(2013, 10)
    >>> q 
    201
    >>> r
    3
    """
    return floordiv(n, d), mod(n, d)

When I run

python3 -m doctest -v lec3video3

I pass 0 testcases. I get this error

    File "lec3video3test", line 7, in lec3video3test
Failed example:
    q, r = divide_exact(2013, 10)
Exception raised:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/doctest.py", line 1329, in __run
        compileflags, 1), test.globs)
      File "<doctest lec3video3test[0]>", line 1, in <module>
        q, r = divide_exact(2013, 10)
    NameError: name 'divide_exact' is not defined

I don't know why because I know my code works. What am I doing wrong? I have tested my code in interactive mode and everything is correct.

3
  • 2
    I got IndentationError: unexpected indent on the first line of your code. Is that a typo? If I remove it, I'm not able to reproduce the issue. You need to make a minimal reproducible example. Commented Dec 25, 2019 at 22:05
  • 1
    Beside the point, but divide_exact does the exact same thing as divmod, which is a Python builtin Commented Dec 25, 2019 at 22:08
  • @wjandrea yeah, it was a typo. my bad, sorry. and I didn't know that divmod was a thing but cool! i was just using the professor's example. thanks! Commented Dec 26, 2019 at 0:58

1 Answer 1

1

There is an IndentationError in the current version of your code. However, I guess that's just an error which happened during copy and paste as you would get a completely different error message, then.

Regarding the NameError you're describing: If I put your given code

""" Our first python source file. """

from operator import floordiv, mod 

def divide_exact(n,d):
    """ Return the quotient and remainder.

    >>> q, r = divide_exact(2013, 10)
    >>> q 
    201
    >>> r
    3
    """
    return floordiv(n, d), mod(n, d)

in a file example.py and call doctest with the following command, I get:

$ python3 -m doctest -v example.py
Trying:
    q, r = divide_exact(2013, 10)
Expecting nothing
ok
Trying:
    q 
Expecting:
    201
ok
Trying:
    r
Expecting:
    3
ok
1 items had no tests:
    example
1 items passed all tests:
   3 tests in example.divide_exact
3 tests in 2 items.
3 passed and 0 failed.
Test passed.

It says

1 items had no tests:
    example

because there are no tests in the module docstring. Other than that, all three tests in the function docstring for divide_exact do pass as expected.

So you will just have to make sure your file is in the correct folder and your command follows the required syntax for the doctest module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.