Skip to course content
Free course

Python Foundations / Module 4 / Edge Cases and Empty Inputs

Module 4 lesson

Edge Cases and Empty Inputs

Unit ID: M04-U08 Estimated active time: 22-30 minutes

Test the boundaries, not only the middle

For a rule accepting integer hours from 0 through 40, test:

  • ordinary valid value: 8;
  • lower boundary: 0;
  • upper boundary: 40;
  • below boundary: -1;
  • above boundary: 41;
  • wrong type: "8";
  • surprising related type: True.

These cases reveal wrong operators such as < instead of <= and weak type checks.

Empty collections

values = []
total = 0

for value in values:
    total = total + value

print(total)

The loop body does not run and the total remains 0. Decide whether that is a valid empty result or whether the task requires an explicit warning.

Functions such as min([]) and max([]) raise ValueError. Check the collection before using operations that require at least one item.

Missing and malformed records

Use .get() when a missing key is an expected validation case:

record = {"id": "R1"}
hours = record.get("hours")

Then validate hours. Do not silently replace a required missing value with zero.

Preserve all reasons when useful

A record may have more than one problem. Use independent checks to collect them:

reasons = []

if type(record.get("id")) is not str or record.get("id", "").strip() == "":
    reasons.append("id must be non-empty text")

hours = record.get("hours")
if type(hours) is not int:
    reasons.append("hours must be an integer")
elif not 0 <= hours <= 40:
    reasons.append("hours must be from 0 to 40")

elif prevents an unsafe numeric comparison after a type failure.

Practice

Write expected outcomes for empty input and for records with missing ID, spaces-only ID, missing hours, boolean hours, zero, 40, and 41. Only then run the validation.

Takeaway

Boundary, empty, missing, and wrong-type cases are part of the requirement. State expected outcomes before running. You are now ready for the complete record-validation checkpoint.