Today's outcomes
- Explain why input returns a string.
- Convert known-valid numeric input.
- Use print arguments and formatted strings.
Before you start: Complete Day 02 and its practical challenge.
Understand the concept
User input enters as text
A program can ask a person for a value with input. The prompt is displayed, the person types a response, and Python returns a string. Typing the digits 30 does not make the returned value an integer. A program that calculates minutes must convert appropriate numeric text before multiplying quantities.
Separate the boundary from the calculation: receive text, interpret it according to a stated rule, calculate with values of suitable types, then format the result. If a field is an identifier, conversion might be inappropriate. If a field is a whole-number count, int can be appropriate for known-valid input. Today, state that numeric input must be valid; Day 17 will handle conversion failures explicitly.
input removes the terminating newline, but it does not remove every space around a response. A name entered as " Alex " can be trimmed with strip. That operation is a policy: leading and trailing spaces are ignored for this display name. It should not be applied silently to every kind of field, such as a password where spaces might be meaningful.
Output needs both a value and an explanation
A bare 2700 is less useful than Planned minutes: 2700. Good output names the quantity, unit, and relevant assumptions. A total calculated from a daily target is planned time, not measured practice time. Clear labels prevent a correct calculation from becoming a misleading report.
print accepts several arguments. Its default separator is a space and its default ending is a newline. You can choose sep and end when the output contract needs another layout. Avoid using a special ending merely to make code shorter; each line should remain easy to inspect.
Formatting should not rewrite the data
An f-string begins with f and inserts expressions inside braces. f"{minutes:.1f}" displays one digit after the decimal point. The format specifier affects the produced string; it does not change the numeric variable. Keep the underlying result available for later calculations instead of converting it permanently into a display string.
For financial work, binary floats are not a universal money representation. This exercise uses integer minute counts and display-only division into hours. Later applications should choose a numeric representation based on their contract. A neat two-decimal output is not proof that an exact decimal calculation occurred.
Worked example
This example uses supplied strings so its result is reproducible without waiting for terminal input. The same conversion steps apply after interactive input.
name_text = " Alex "
days_text = "30"
minutes_text = "90"
name = name_text.strip()
days = int(days_text)
minutes_per_day = int(minutes_text)
total_minutes = days * minutes_per_day
total_hours = total_minutes / 60
print(f"Learner: {name}")
print(f"Plan: {days} days at {minutes_per_day} minutes per day")
print(f"Planned hours: {total_hours:.1f}")
print("Fields", "name", "days", "minutes", sep=" | ")
| Step | Before | After | Meaning |
|---|---|---|---|
| Trim display name | " Alex " |
"Alex" |
Ignore surrounding spaces for this field |
| Convert days | "30" |
30 | Use a whole-number quantity |
| Calculate minutes | 30 and 90 | 2700 | Planned duration in minutes |
| Convert unit | 2700 / 60 | 45.0 | Same duration expressed in hours |
| Format report | 45.0 | Planned hours: 45.0 |
Add a label and one decimal place |
The final line is Fields | name | days | minutes. Those words are literal labels, not the variable values. Read quotation marks carefully before predicting a print statement.
For interactive use, run this separate script and type Alex, 30, and 90 at its prompts:
# Interactive example: supply a name, then two valid integer quantities.
name = input("Display name: ").strip()
days = int(input("Planned days: "))
minutes = int(input("Minutes per day: "))
print(f"{name}: {days * minutes} planned minutes")
Guided practice
- Predict: what happens if days_text stays a string and you calculate
days_text * 2? The result is"3030", a repeated string. Explain why that result is valid Python but wrong for a two-plan duration calculation. - Modify: use 7 days and 45 minutes per day. Expect 315 minutes and 5.25 hours. Display hours to two decimal places and confirm that the numeric total_hours remains available.
- Reconstruct: write a report with name, daily target, and three-day total. Use one print call with an f-string for each output line. Then write the same result with multiple print arguments. Compare readability rather than assuming one form is always better.
Before running an interactive script, list the exact responses you will provide. This makes the run reproducible and helps distinguish a conversion mistake from an accidental input change.
Daily challenge
Build a study receipt from three supplied input strings: " Mira ", "7", and "45". Print the cleaned display name, planned minutes, and planned hours with two decimal places. Preserve the integer minute total as a number. After checking the supplied-input version, replace the assignments with prompts and repeat the same case manually.
| Case | Required result |
|---|---|
| Mira, 7 days, 45 minutes | 315 minutes; 5.25 hours |
| Alex, 1 day, 60 minutes | 60 minutes; 1.00 hours |
| Alex, 0 days, 60 minutes | 0 minutes; 0.00 hours |
Zero days is allowed in today's calculation contract. An empty name and invalid numeric text are outside today's accepted-input assumptions. Write those limitations down rather than pretending the script handles every response.
Reference solution
Compare after attempting the challenge
name_text = " Mira "
days_text = "7"
minutes_text = "45"
name = name_text.strip()
days = int(days_text)
minutes = int(minutes_text)
planned_minutes = days * minutes
planned_hours = planned_minutes / 60
print(f"Learner: {name}")
print(f"Planned minutes: {planned_minutes}")
print(f"Planned hours: {planned_hours:.2f}")
print("Numeric total retained:", type(planned_minutes).__name__)
The additional type-inspection line is evidence for the numerical-state requirement, not part of the three-line receipt itself. Remove it if a downstream consumer expects exactly three lines. Output contracts include structure as well as correct values.
Debug and explain
| Symptom | Likely cause | Investigation |
|---|---|---|
| Braces appear literally | Missing f prefix | Check the string prefix |
| Text repeats instead of multiplying numerically | A quantity is still a string | Inspect its type before calculation |
| ValueError during conversion | Input does not follow the stated integer contract | Record the response; handling arrives on Day 17 |
| Expected output has unexpected spaces | print separator or original input | Inspect sep and repr of the raw string |
Explain the four stages: receiving, interpreting, calculating, and displaying. Why is stripping a display name different from stripping a password? Why does formatting to two decimals not turn the underlying number into exact decimal arithmetic? Tomorrow you will compare more operators and express eligibility rules.
Review and next step
Save the supplied-input run and a manual interactive run with the same values. Rebuild the conversion pipeline from memory at the next review. Move on when you can distinguish text, numeric state, and formatted output without relying on what the screen happens to look like.
Sources and verification
Original teaching examples checked with Python 3.12.14. Library behavior reviewed against these primary references on October 3, 2026. Day 26's examples also use Beautiful Soup 4.14.3.
Check your understanding
Five questions, 20 points each. Pass at 85% or above, which requires all five correct. Feedback explains each answer; revise the lesson and retry when needed.
Review your practical work
Check these statements after running your own challenge. This is your self-review; the website does not execute or assess your Python code.
Pass the knowledge check and finish the practical self-review to save completion.