diff --git a/docs/internal/known-issues.md b/docs/internal/known-issues.md index 51bba2d..2e035f3 100644 --- a/docs/internal/known-issues.md +++ b/docs/internal/known-issues.md @@ -9,7 +9,9 @@ which also covers deliberate non-goals. **3 open:** 2 medium, 1 low. -## 1. A total time of zero divides by zero and exits with a traceback +## 1. A total time of zero divides by zero and exits with a traceback — RESOLVED + +**Resolution:** Re-prompt for zero or negative total time, negative values, and non-finite input before calculating speed. **Severity:** Medium **Where:** `main.py` -> `main`, `metersps = distance / time` diff --git a/main.py b/main.py index 8aafd44..ee59d9c 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ # Running Calculator import sys +import math def calculate_distance(distance, unit): @@ -159,6 +160,11 @@ def main(): input("How many minutes did it take you? (if < 1, enter 0) ") ) secs = float(input("How many seconds did it take you? ")) + if (not math.isfinite(length) or not math.isfinite(secs) + or min(length, hours, mins, secs) < 0 + or hours * 3600 + mins * 60 + secs <= 0): + print("Use nonnegative values and a total time greater than zero.") + time_run = True except ValueError: print( "Values must be in numerical form, " diff --git a/tests/test_main.py b/tests/test_main.py index a2848ab..86f92f1 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -5,9 +5,11 @@ import sys import os +import pytest + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from main import calculate_distance, display_result +from main import calculate_distance, display_result, main def test_calculate_distance(): @@ -40,3 +42,14 @@ def test_display_result(capsys): assert ( "Your speed was 10 meters per second or 36 kilometers per hour." in captured.out ) + + +def test_zero_time_reprompts_and_valid_input_still_calculates(monkeypatch, capsys): + """Recover from a zero time and calculate the corrected speed.""" + answers = iter(["meters", "1000", "0", "0", "0", "1000", "0", "1", "0", "quit"]) + monkeypatch.setattr("builtins.input", lambda _: next(answers)) + with pytest.raises(SystemExit): + main() + output = capsys.readouterr().out + assert "total time greater than zero" in output + assert "16.67 meters per second" in output