diff --git a/solutions/fuel.py b/solutions/fuel.py index 7b6c29274..8c6e2bb44 100644 --- a/solutions/fuel.py +++ b/solutions/fuel.py @@ -16,16 +16,14 @@ def main(): """ Continuously prompts the user for a fraction input, converts it to a percentage, and displays the result. - """ while True: try: - pcent = input("Fraction: ") - print(gauge(convert(pcent))) + fraction = input("Fraction: ") + print(gauge(convert(fraction))) break - except AssertionError: - # Ignore invalid inputs and re-prompt the user - pass + except AssertionError as e: + print(e) # Display assertion error message and re-prompt the user def convert(fraction): @@ -39,14 +37,16 @@ def convert(fraction): int: The percentage equivalent of the fraction, rounded to the nearest integer. Raises: - AssertionError: If the numerator is greater than the denominator, or if the input is invalid. - - >>> convert("1/2") - 50 - >>> convert("3/4") - 75 - >>> convert("2/5") - 20 + AssertionError: If the numerator is greater than the denominator, + or if the input is invalid. + + Examples: + >>> convert("1/2") + 50 + >>> convert("3/4") + 75 + >>> convert("2/5") + 40 """ x, y = fraction.split("/") @@ -57,16 +57,14 @@ def convert(fraction): x, y = int(x), int(y) - # Assert that denominator is not zero assert y != 0, "Denominator cannot be zero." - assert y > 0, "No negative values allowed" - # Assert that numerator is not greater than denominator + assert x >= 0 and y > 0, "Numerator and denominator must be non-negative." + assert x <= y, "Numerator cannot be greater than denominator." - assert x >= 0, "No negative values allowed" # Calculate percentage and round to nearest integer - return int(round((x / y) * 100, 0)) + return round((x / y) * 100) def gauge(percentage): @@ -78,17 +76,19 @@ def gauge(percentage): Returns: str: 'F' if the percentage is greater than 99, 'E' if less than 1, - otherwise the percentage followed by '%'. - - >>> gauge(50) - 50% - >>> gauge(99.5) - F - >>> gauge(0) - E + otherwise the percentage followed by '%'. + Raises: + AssertionError: If the percentage is not within the range 0-100. + + Examples: + >>> gauge(50) + '50%' + >>> gauge(100) + 'F' + >>> gauge(0) + 'E' """ - # Assert that percentage is within valid range assert 0 <= percentage <= 100, "Percentage must be between 0 and 100."