Skip to content

Commit 793344f

Browse files
committed
test_fuel
1 parent b6d5338 commit 793344f

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

Problem Set 3/fuel/fuel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
pass
1212

1313
p = round(f*100)
14-
if p == 100:
14+
if p == 99:
1515
print("F")
1616
elif p <= 1:
1717
print("E")

Problem Set 5/test_fuel/fuel.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def main():
2+
fuel = input("Fraction: ")
3+
percentage = convert(fuel)
4+
print(gauge(percentage))
5+
6+
7+
def convert(fraction):
8+
while True:
9+
try:
10+
x, y = fraction.split("/")
11+
new_x = int(x)
12+
new_y = int(y)
13+
f = new_x / new_y
14+
if f <= 1:
15+
p = round(f*100)
16+
return p
17+
else:
18+
fraction = input("Fraction: ")
19+
pass
20+
except (ValueError, ZeroDivisionError):
21+
raise
22+
23+
24+
def gauge(percentage):
25+
if percentage == 99:
26+
return "F"
27+
elif percentage <= 1:
28+
return "E"
29+
else:
30+
return f"{percentage}%"
31+
32+
33+
if __name__ == "__main__":
34+
main()

Problem Set 5/test_fuel/test_fuel.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
from fuel import convert, gauge
3+
4+
def main():
5+
test_correct_input()
6+
test_zero_division()
7+
test_value_error()
8+
9+
10+
def test_correct_input():
11+
# Test valid inputs and their expected outputs
12+
assert convert('1/4') == 25 and gauge(25) == '25%'
13+
assert convert('1/100') == 1 and gauge(1) == 'E'
14+
assert convert('99/100') == 99 and gauge(99) == 'F'
15+
16+
def test_zero_division():
17+
# Test division by zero
18+
with pytest.raises(ZeroDivisionError):
19+
convert('1/0')
20+
21+
def test_value_error():
22+
with pytest.raises(ValueError):
23+
convert('cat/dog')
24+
25+
if __name__ == "__main__":
26+
main()

0 commit comments

Comments
 (0)