My doctest is not working as I expect, without doctest it is working properly










0














I'm pretty new to python and just learning how to debug and test my code.
We had to write a little BMI calculator for our course. However, if I try to run it with the tests I get the error:



Please enter only non-negative numbers.
Failure <Click to see difference>

**********************************************************************
File "C:/Users/TAGI/PycharmProjects/703410_U7_Tests/Programs/BMI/task.py", line 39, in task.bmi
Failed example:
bmi(-1, 200)
Expected:
Please enter only non-negative numbers.
Got:
Please enter only non-negative numbers.
-0.25


Why is it still calculating the result with the negative number after I raised the exception? If I comment my doctests out and try it without it works just fine. This is my code (with commented out doctests):



def check_variables(a):
try:
float(a)
if a < 0:
raise ValueError
except ValueError:
print("Please enter only non-negative numbers.")
except:
print("This is an error: something went wrong. Please try again.")
return a


def bmi(a_weight, a_height):
# TODO: write docstring for this function
"""
This function returns the bmi of a person and tests the values. If they are invalid it returns an error.
:param a_weight: a real number, the users weight
:param a_height: a real number, the users height
:return: bmi of user
:rtype: float

# >>> bmi(10,0)
Traceback (most recent call last):
...
ZeroDivisionError: float division by zero
# >>> round(bmi(75,175), 1)
24.5
# >>> bmi("abc",177)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for /: 'str' and 'float'
# >>> bmi(0, 180)
0.0
# >>> bmi(-1, 200)
Please enter only non-negative numbers.
# >>> bmi(100, -40)
Please enter only non-negative numbers.

# TODO: write doctest for this function
"""
return check_variables(a_weight) / (check_variables(a_height)/100) ** 2


bmi(-10, 180)









share|improve this question

















  • 1




    You are still returning a value if there's an error.
    – Klaus D.
    Nov 12 at 15:21















0














I'm pretty new to python and just learning how to debug and test my code.
We had to write a little BMI calculator for our course. However, if I try to run it with the tests I get the error:



Please enter only non-negative numbers.
Failure <Click to see difference>

**********************************************************************
File "C:/Users/TAGI/PycharmProjects/703410_U7_Tests/Programs/BMI/task.py", line 39, in task.bmi
Failed example:
bmi(-1, 200)
Expected:
Please enter only non-negative numbers.
Got:
Please enter only non-negative numbers.
-0.25


Why is it still calculating the result with the negative number after I raised the exception? If I comment my doctests out and try it without it works just fine. This is my code (with commented out doctests):



def check_variables(a):
try:
float(a)
if a < 0:
raise ValueError
except ValueError:
print("Please enter only non-negative numbers.")
except:
print("This is an error: something went wrong. Please try again.")
return a


def bmi(a_weight, a_height):
# TODO: write docstring for this function
"""
This function returns the bmi of a person and tests the values. If they are invalid it returns an error.
:param a_weight: a real number, the users weight
:param a_height: a real number, the users height
:return: bmi of user
:rtype: float

# >>> bmi(10,0)
Traceback (most recent call last):
...
ZeroDivisionError: float division by zero
# >>> round(bmi(75,175), 1)
24.5
# >>> bmi("abc",177)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for /: 'str' and 'float'
# >>> bmi(0, 180)
0.0
# >>> bmi(-1, 200)
Please enter only non-negative numbers.
# >>> bmi(100, -40)
Please enter only non-negative numbers.

# TODO: write doctest for this function
"""
return check_variables(a_weight) / (check_variables(a_height)/100) ** 2


bmi(-10, 180)









share|improve this question

















  • 1




    You are still returning a value if there's an error.
    – Klaus D.
    Nov 12 at 15:21













0












0








0







I'm pretty new to python and just learning how to debug and test my code.
We had to write a little BMI calculator for our course. However, if I try to run it with the tests I get the error:



Please enter only non-negative numbers.
Failure <Click to see difference>

**********************************************************************
File "C:/Users/TAGI/PycharmProjects/703410_U7_Tests/Programs/BMI/task.py", line 39, in task.bmi
Failed example:
bmi(-1, 200)
Expected:
Please enter only non-negative numbers.
Got:
Please enter only non-negative numbers.
-0.25


Why is it still calculating the result with the negative number after I raised the exception? If I comment my doctests out and try it without it works just fine. This is my code (with commented out doctests):



def check_variables(a):
try:
float(a)
if a < 0:
raise ValueError
except ValueError:
print("Please enter only non-negative numbers.")
except:
print("This is an error: something went wrong. Please try again.")
return a


def bmi(a_weight, a_height):
# TODO: write docstring for this function
"""
This function returns the bmi of a person and tests the values. If they are invalid it returns an error.
:param a_weight: a real number, the users weight
:param a_height: a real number, the users height
:return: bmi of user
:rtype: float

# >>> bmi(10,0)
Traceback (most recent call last):
...
ZeroDivisionError: float division by zero
# >>> round(bmi(75,175), 1)
24.5
# >>> bmi("abc",177)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for /: 'str' and 'float'
# >>> bmi(0, 180)
0.0
# >>> bmi(-1, 200)
Please enter only non-negative numbers.
# >>> bmi(100, -40)
Please enter only non-negative numbers.

# TODO: write doctest for this function
"""
return check_variables(a_weight) / (check_variables(a_height)/100) ** 2


bmi(-10, 180)









share|improve this question













I'm pretty new to python and just learning how to debug and test my code.
We had to write a little BMI calculator for our course. However, if I try to run it with the tests I get the error:



Please enter only non-negative numbers.
Failure <Click to see difference>

**********************************************************************
File "C:/Users/TAGI/PycharmProjects/703410_U7_Tests/Programs/BMI/task.py", line 39, in task.bmi
Failed example:
bmi(-1, 200)
Expected:
Please enter only non-negative numbers.
Got:
Please enter only non-negative numbers.
-0.25


Why is it still calculating the result with the negative number after I raised the exception? If I comment my doctests out and try it without it works just fine. This is my code (with commented out doctests):



def check_variables(a):
try:
float(a)
if a < 0:
raise ValueError
except ValueError:
print("Please enter only non-negative numbers.")
except:
print("This is an error: something went wrong. Please try again.")
return a


def bmi(a_weight, a_height):
# TODO: write docstring for this function
"""
This function returns the bmi of a person and tests the values. If they are invalid it returns an error.
:param a_weight: a real number, the users weight
:param a_height: a real number, the users height
:return: bmi of user
:rtype: float

# >>> bmi(10,0)
Traceback (most recent call last):
...
ZeroDivisionError: float division by zero
# >>> round(bmi(75,175), 1)
24.5
# >>> bmi("abc",177)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for /: 'str' and 'float'
# >>> bmi(0, 180)
0.0
# >>> bmi(-1, 200)
Please enter only non-negative numbers.
# >>> bmi(100, -40)
Please enter only non-negative numbers.

# TODO: write doctest for this function
"""
return check_variables(a_weight) / (check_variables(a_height)/100) ** 2


bmi(-10, 180)






python doctest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 15:11









tagi

11




11







  • 1




    You are still returning a value if there's an error.
    – Klaus D.
    Nov 12 at 15:21












  • 1




    You are still returning a value if there's an error.
    – Klaus D.
    Nov 12 at 15:21







1




1




You are still returning a value if there's an error.
– Klaus D.
Nov 12 at 15:21




You are still returning a value if there's an error.
– Klaus D.
Nov 12 at 15:21

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265011%2fmy-doctest-is-not-working-as-i-expect-without-doctest-it-is-working-properly%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265011%2fmy-doctest-is-not-working-as-i-expect-without-doctest-it-is-working-properly%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto