DEV Community

Discussion on: Solving the Problem Sets of CS50's Introduction to Programming with Python — One at a Time: Problem Set 5

Collapse
 
raphaeluziel profile image
Raphael Uziel

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 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!

Thanks!

Collapse
 
rivea0 profile image
Eda

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.

Collapse
 
raphaeluziel profile image
Raphael Uziel

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().

Thread Thread
 
rivea0 profile image
Eda

I'm glad it helped!