Preparing git repo for final project
This commit is contained in:
35
problems/pset5/test_fuel/fuel.py
Normal file
35
problems/pset5/test_fuel/fuel.py
Normal 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()
|
41
problems/pset5/test_fuel/test_fuel.py
Normal file
41
problems/pset5/test_fuel/test_fuel.py
Normal 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()
|
Reference in New Issue
Block a user