Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/internal/known-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 6 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Running Calculator
import sys
import math


def calculate_distance(distance, unit):
Expand Down Expand Up @@ -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, "
Expand Down
15 changes: 14 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Loading