Preparing git repo for final project

This commit is contained in:
2023-07-09 11:19:26 +02:00
parent 6a38966eef
commit 63d06d6b35
67 changed files with 1587 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
def main():
while True:
fuel = input("Fraction:\t\t")
try:
result = convert(fuel)
break
except (ValueError, ZeroDivisionError):
continue
print(gauge(result))
def convert(fraction: str) -> int:
try:
x, y = map(int, fraction.split('/'))
except (ValueError, TypeError):
raise ValueError
if x > y:
raise ValueError
if y == 0:
raise ZeroDivisionError
return int(round(x / y*100, 0))
def gauge(percentage):
if percentage <= 1:
return "E"
elif percentage >= 99:
return "F"
else:
return f"{percentage}%"
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,41 @@
from fuel import convert, gauge
def main():
test_convert()
test_gauge()
test_value_error()
test_zero_division_error()
def test_convert():
assert convert("0/4") == 0
assert convert("1/4") == 25
assert convert("4/4") == 100
def test_value_error():
try:
convert("5/4")
except ValueError:
pass
else:
raise Exception("ValueError not raised with y>x")
def test_zero_division_error():
try:
convert("5/0")
except ZeroDivisionError:
pass
else:
raise Exception("ZeroDivisionError not raised with y = 0")
def test_gauge():
assert gauge(100) == 'F'
assert gauge(99) == 'F'
assert gauge(0) == 'E'
assert gauge(1) == 'E'
assert gauge(27) == '27%'
if __name__ == '__main__':
main()