Skip to content

Commit

Permalink
solved some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
theabdallahnjr committed Jan 10, 2025
1 parent e7bc9b9 commit 8c0e771
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions solutions/fuel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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("/")

Expand All @@ -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):
Expand All @@ -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."

Expand Down

0 comments on commit 8c0e771

Please sign in to comment.