From 6ce1b7ba7c7564121ca1d4c3898223f81027e054 Mon Sep 17 00:00:00 2001 From: William <18339050+willtheorangeguy@users.noreply.github.com> Date: Sat, 24 Oct 2026 23:14:40 -0600 Subject: [PATCH 1/2] Reject zero and invalid running times before calculating speed --- docs/internal/known-issues.md | 4 +++- main.py | 6 ++++++ tests/test_main.py | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) 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..7327b71 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -40,3 +40,16 @@ 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): + import pytest + from main import main + + 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 From e208cb223de3675b703e0913b56a15fb4e8b580b Mon Sep 17 00:00:00 2001 From: William <18339050+willtheorangeguy@users.noreply.github.com> Date: Sat, 24 Oct 2026 23:24:10 -0600 Subject: [PATCH 2/2] Follow the existing Pylint conventions in the regression test --- tests/test_main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 7327b71..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(): @@ -43,9 +45,7 @@ def test_display_result(capsys): def test_zero_time_reprompts_and_valid_input_still_calculates(monkeypatch, capsys): - import pytest - from main import main - + """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):