-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_conditionals.py
More file actions
84 lines (65 loc) · 2.75 KB
/
Copy path02_conditionals.py
File metadata and controls
84 lines (65 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Small examples demonstrating conditional branches (if / elif / else).
This module shows two short examples using conditionals:
- classify_age: decide what message to return based on an integer `age`.
- tired_response: map a simple yes/no answer into a helpful message.
The functions are written to *return* strings so they are easy to test
and reuse. The `main()` function handles user input and prints results.
"""
def classify_age(age: int) -> str:
"""Return a short classification message for the given age.
Notes about the conditional logic here:
- The order matters: checks are evaluated top-to-bottom, so more
specific conditions go first (e.g., age < 18).
- We use `elif` to chain mutually exclusive branches; `else` is the
final fallback when previous conditions were not met.
Args:
age: integer age (expected >= 0, but negative values handled)
Returns:
A human-readable message describing the age group.
"""
if age < 0:
# Defensive check - negative ages don't make sense in this example.
return "Age cannot be negative."
if age < 18:
return "You are a minor."
elif age < 21:
return "You are an adult but not 21 yet."
else:
return "You are 21 or older."
def tired_response(answer: str) -> str:
"""
Interpret a user's yes/no input and return an appropriate message.
This function demonstrates checking membership in a tuple of valid
options and shows a friendly fallback for unrecognized answers.
Args:
answer: user-provided string, expected "y"/"n" or "yes"/"no" (case-insensitive)
Returns:
A helpful short message depending on the input.
"""
normalized = answer.strip().lower()
if normalized in ("y", "yes"):
return "Consider going to bed earlier tonight."
elif normalized in ("n", "no"):
return "Nice, keep the momentum going."
else:
return "I did not understand that, but remember to listen to your body."
def main() -> None:
"""Interactive entry point for the example program.
This function collects input from the user, calls the pure helper
functions above, and prints their returned messages. It also shows
a small example of input validation and graceful error handling.
"""
raw_age = input("Enter your age: ")
try:
age = int(raw_age)
except ValueError:
# Input wasn't a valid integer — show an error and stop.
print("Please enter a valid integer for age (for example: 24).")
return
# call the pure function and print its result — this makes testing easier.
print(classify_age(age))
tired_input = input("Are you tired? (y/n): ")
print(tired_response(tired_input))
if __name__ == "__main__":
main()