I've struggled with this Refueling part for the past couple of days. Maybe I'm going about it all wrong, but I just don't get it! Here's the issue:
If, for example I use a try/except statement to catch a ZeroDivision error, the fuel.py works, and passes check50, but pytest gives me a Failed: DID NOT RAISE <class 'ZeroDivisionError'> error, and I guess that is why check50 doesn't get past the :) test_fuel.py exist check.
I read that the except, because it handles the exception raised, will not throw the exception to the caller, which I guess is pytest. Okay, so I change my code so that it raises the ZeroDivisionError instead. Now, pytest passes all tests, but of course, fuel.py does not since instead of prompting the user again if the user inputs '100/0', it will stop the execution of the program, raising the ZeroDivisionError.
My guess is that I'm missing something very basic, and that there is no bug, but if anyone has actually solved this, any help would be appreciated!
I love learning math (*to some extent*) and CS theory; good literature, and the technology that provides me with all kinds of resources for those three: the web.
Hi Raphael,
Like you said, the main reason that pytest gives that error is that you need to raise ZeroDivisionError instead of catching it with try...except. Since you fixed it, I think the error might not be in the test file, but in fuel.py. For example, we need to keep getting user input only inside the main function, so, an infinite loop is reasonable to use there. And, only when x is less than or equal to y you should break out of it. That way, I think it would not stop the execution when it encounters an error. Because the convert function should only do the converting, and gauge should just return a formatted result string, the issue might be inside the main function.
Thanks so much for your help, Eda, I don't think I would have ever figured this out on my own! I was already doing the while loop inside the main function, but I was catching the exceptions in the convert() function instead of main().
I love learning math (*to some extent*) and CS theory; good literature, and the technology that provides me with all kinds of resources for those three: the web.
Issue with refueling problem
I've struggled with this Refueling part for the past couple of days. Maybe I'm going about it all wrong, but I just don't get it! Here's the issue:
If, for example I use a try/except statement to catch a ZeroDivision error, the fuel.py works, and passes check50, but pytest gives me a
Failed: DID NOT RAISE <class 'ZeroDivisionError'>
error, and I guess that is why check50 doesn't get past the :) test_fuel.py exist check.I read that the except, because it handles the exception raised, will not throw the exception to the caller, which I guess is pytest. Okay, so I change my code so that it raises the
ZeroDivisionError
instead. Now, pytest passes all tests, but of course, fuel.py does not since instead of prompting the user again if the user inputs '100/0', it will stop the execution of the program, raising theZeroDivisionError
.My guess is that I'm missing something very basic, and that there is no bug, but if anyone has actually solved this, any help would be appreciated!
Thanks!
Hi Raphael,
Like you said, the main reason that
pytest
gives that error is that you need to raiseZeroDivisionError
instead of catching it withtry...except
. Since you fixed it, I think the error might not be in the test file, but infuel.py
. For example, we need to keep getting user input only inside themain
function, so, an infinite loop is reasonable to use there. And, only whenx
is less than or equal toy
you should break out of it. That way, I think it would not stop the execution when it encounters an error. Because theconvert
function should only do the converting, andgauge
should just return a formatted result string, the issue might be inside themain
function.Thanks so much for your help, Eda, I don't think I would have ever figured this out on my own! I was already doing the
while
loop inside the main function, but I was catching the exceptions in theconvert()
function instead ofmain()
.I'm glad it helped!