-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction_module.py
62 lines (51 loc) · 1.6 KB
/
fraction_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def main():
"""
Main function to take input from the user, convert the fraction,
and print the gauge value.
"""
fraction = input("Fraction: ")
try:
percentage = convert(fraction)
result = gauge(percentage)
print(result)
except ValueError as e:
print(f"Error: {e}")
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
def convert(fraction: str) -> int:
"""
Converts a fraction string to a percentage integer.
Parameters:
fraction (str): The fraction as a string in the form 'x/y'.
Returns:
int: The fraction as a percentage rounded to the nearest integer.
Raises:
ValueError: If the input is not a valid fraction or if the numerator
is greater than the denominator.
ZeroDivisionError: If the denominator is zero.
"""
try:
x, y = map(int, fraction.split("/"))
except ValueError:
raise ValueError("Fraction must be in the form 'x/y' where x and y are integers.")
if y == 0:
raise ZeroDivisionError("Denominator cannot be zero.")
if x > y:
raise ValueError("Numerator cannot be greater than denominator.")
return round((x / y) * 100)
def gauge(percentage: int) -> str:
"""
Converts a percentage integer to a gauge string.
Parameters:
percentage (int): The percentage as an integer.
Returns:
str: The gauge value as a string.
"""
if percentage <= 1:
return "E"
elif percentage >= 99:
return "F"
else:
return f"{percentage}%"
if __name__ == "__main__":
main()