Session 3 of the Python Fundamentals series, covered in depth. It makes the program listen with input() and talk back with print(). The central rule is that input() ALWAYS returns a string, so doing math on it requires int() or float() first. The session covers the prompt, the string trap, conversion with int() and float() and the ValueError it can raise, and building output with str() and f-strings, including {value:.2f} formatting, then works several read-compute-print programs. The snippets were verified under CPython 3.12, with input shown as the value the user types.
Subject: Python Fundamentals · 98 slides · code lesson
Open the interactive version of this deck · Homework for this lesson
Title
Python Fundamentals - Session 3
Ask a question, listen to the answer, talk back
Objectives
So far the program only talks. Now it will listen - and this is where programs start to feel interactive. By the end you can:
input() to ask the user a question and store their answer.input() always hands you text - even for digits.int() / float() so you can do math, and read a ValueError.str().{value:.2f} for money.Warm-up
Discussion prompt
Before we open Session 3 - Input & Output: without looking back, what was the main idea of Session 2 - Variables & Types, and what could you do by the end of it that you could not do before?
Hint: One sentence for the idea, one for the skill. If the second one is blank, that is the part to revisit.
Answer:
That session covers assignment and reassignment, the four core types, the type() function, and why "100" is not the number 100.
Section
Part 1
Concept
input("...") shows your message, then pauses and waits for the user to type and press Enter.
Whatever they typed is handed back to your program, so you store it in a variable.
prompt — The message inside input("...") that tells the user what to type. It is shown on screen, then the program waits for an answer.
Counterexample
Discussion prompt
input("...") shows your message, then pauses and waits for the user to type and press Enter.
That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.
Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.
Answer:
Whatever they typed is handed back to your program, so you store it in a variable.
Concept
The answer is only useful if you keep it: name = input("Your name? ").
Read it right-to-left like any assignment: run the input, get the answer, then attach the name to it.
Analogy
Discussion prompt
Explain Store the answer in a variable by analogy to something with no Python Fundamentals in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.
Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.
Answer:
The answer is only useful if you keep it: name = input("Your name? ").
Worked example
The user runs this and types Sam:
name = input("What's your name? ")
print("Welcome,", name)Line 1 shows the prompt and waits
Why: input pauses; whatever the user types becomes the value of name.
Line 2 uses that value
Why: Verified by execution: with Sam typed, prints Welcome, Sam.
| user types | name becomes | prints |
|---|---|---|
| Sam | "Sam" | Welcome, Sam |
Blank canvas
Draw it
Draw what A simple greeter just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.
Concept
The text inside input("...") is an ordinary string - end it with a space so the user's typing does not touch your question.
input("Age? ") reads nicer than input("Age?") because of that trailing space.
Explain it
Discussion prompt
Explain The prompt is just a string to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.
Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.
Answer:
The text inside input("...") is an ordinary string - end it with a space so the user's typing does not touch your question.
Concept
This is the one rule that trips up every beginner: input() gives back a str - always.
Even if the user types 12, you get the text "12", not the number 12. You can print it, but you cannot do math on it yet.
Intuition
Think about what a keyboard sends: individual characters. Press 1 then 2 and the program receives the two characters "12" - text that happens to look like a number.
Python will not guess whether you wanted a number. It hands you the text and leaves the choice to you: need it as a number? then convert it.
Pattern
Predict first
The table runs: 12 | "12" | str · hello | "hello" | str
In Proof: it is a str, given the rows so far: what is the next one — the row where user types is 3.5?
Correct: 3.5 | "3.5" | str
| user types | age becomes | type(age) |
|---|---|---|
| 12 | "12" | str |
| hello | "hello" | str |
| 3.5 | "3.5" | str |
Why: The relationship between the columns, not the individual numbers, is what generates the next row. Digits, words, decimals - all arrive as a str.
Worked example
The user types 12:
age = input("How old are you? ")
print(type(age))Whatever is typed comes back as text
Why: Digits, words, decimals - all arrive as a str.
type() confirms it every time
Why: Verified by execution: <class 'str'>.
| user types | age becomes | type(age) |
|---|---|---|
| 12 | "12" | str |
| hello | "hello" | str |
| 3.5 | "3.5" | str |
Comparison
Comparison matrix
From Proof: it is a str: refill the type(age) column from what you know. The rest of the table is as it appeared.
| user types | age becomes | type(age) |
|---|---|---|
| 12 | "12" | str |
| hello | "hello" | str |
| 3.5 | "3.5" | str |
Anomaly
Predict first
A student writes this, and it looks reasonable:
Adding to input() directly. The user types 5.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: input() returned a string. Adding the number 1 to text stops the program.
Wrap the input in int() to get a real number.
Why: input() returned a string. Adding the number 1 to text stops the program.
Trap
Adding to input() directly. The user types 5.
age = input("Age? ")
print(age + 1)age is the TEXT "5", so age + 1 mixes a str and an int
Why: input() returned a string. Adding the number 1 to text stops the program.
| expression | result |
|---|---|
| "5" + 1 | TypeError: can only concatenate str (not "int") to str |
Wrap the input in int() to get a real number.
age = int(input("Age? "))
print(age + 1)int(input(...)) converts the text to a number
Why: With 5 typed, int("5") is 5, and 5 + 1 is 6. Real output: 6.
| user types | age becomes | age + 1 |
|---|---|---|
| 5 | 5 (int) | 6 |
Section
Part 2
Concept
Wrap input() in int() when you need a whole number: age = int(input("Age? ")).
The input() runs first and returns text; int() then turns that text into a number - all in one line.
Ranking
Put in order
Put the moves of Next year's age into the order they have to happen.
Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. input() returns "12"; int("12") makes it 12.
Worked example
The user types 12:
age = int(input("How old are you? "))
next_year = age + 1
print("Next year you will be", next_year)Line 1: int() wraps the input, so age is a number
Why: input() returns "12"; int("12") makes it 12.
Line 2: now the math works
Why: 12 + 1 is 13.
Trace each value
Why: Verified by execution: prints Next year you will be 13.
| line | variable | value | type |
|---|---|---|---|
| 1 | age | 12 | int |
| 2 | next_year | 13 | int |
| 3 | (printed) | Next year you will be 13 | - |
Trade off
Comparison matrix
From Next year's age: every row here is a choice with a cost. Fill the type column, then say which row you would actually pick and what you give up for it.
| line | variable | value | type |
|---|---|---|---|
| 1 | age | 12 | int |
| 2 | next_year | 13 | int |
| 3 | (printed) | Next year you will be 13 | - |
Concept
Use float() instead when the answer can have a decimal point: a price, a temperature, a measurement.
float("3.5") is 3.5; float("4") is 4.0. It accepts whole-number text too.
Worked example
The user types 4.5:
price = float(input("Price? "))
print(price * 2)float() reads the decimal text
Why: float("4.5") is 4.5 - a number you can multiply.
The math runs
Why: Verified by execution: 4.5 * 2 is 9.0 (a float stays a float).
| user types | price | price * 2 |
|---|---|---|
| 4.5 | 4.5 | 9.0 |
Concept
Use int() when the value is naturally whole - a count, an age, a score. Use float() when a decimal is possible - money, averages, measurements.
One catch: int() rejects decimal text like "3.5". If the input might have a dot, reach for float().
Anomaly
Predict first
A student writes this, and it looks reasonable:
Using int() on text that has a decimal point. The user types 3.5.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: "3.5" has a decimal point, which int() refuses.
Use float() for anything that might have a decimal.
Why: "3.5" has a decimal point, which int() refuses. The program stops with a ValueError.
Trap
Using int() on text that has a decimal point. The user types 3.5.
n = int(input("A number: "))int() only parses whole-number text
Why: "3.5" has a decimal point, which int() refuses. The program stops with a ValueError.
| input | result |
|---|---|
| 3.5 | ValueError: invalid literal for int() with base 10: '3.5' |
Use float() for anything that might have a decimal.
n = float(input("A number: "))float() accepts decimals (and whole numbers too)
Why: float("3.5") is 3.5, and float("4") is 4.0. Real outcome: no error, n is a float.
| input | float(...) |
|---|---|
| 3.5 | 3.5 |
| 4 | 4.0 |
Break the constraint
Discussion prompt
The rule this trap just fixed:
Use float() for anything that might have a decimal.
Now break it on purpose. Build a case that violates it and follow the consequences until something visibly fails. Where does the failure first show up — and would you have noticed it if you had not been looking?
Hint: The dangerous rules are the ones whose violation still produces an answer. If yours fails loudly, try to find one that fails quietly.
Answer:
"3.5" has a decimal point, which int() refuses. The program stops with a ValueError.
Concept
ValueError means the value was the wrong kind for the operation - here, text that is not a valid number.
int("hello") fails the same way: there is no number to read. Session 12 shows how to catch this so a typo does not crash the program.
Worked example
print(int("hello"))There is no number inside "hello"
Why: int() has nothing valid to convert, so it raises a ValueError and stops.
| expression | result |
|---|---|
| int("hello") | ValueError: invalid literal for int() with base 10: 'hello' |
The lesson
Why: Only convert text that really is a number. We will guard against bad input later with try / except.
Explain it to yourself
Discussion prompt
In Two inputs: rectangle area this move is made:
Then compute and show
Why is that legal? Name the rule or definition it rests on before you read on.
Hint: If you can only say "because that is what you do", the rule is the thing to go and find.
Answer:
Verified by execution: 12 * 8 is 96, prints Area: 96.
Worked example
The user types 12, then 8:
length = int(input("Length? "))
width = int(input("Width? "))
print("Area:", length * width)Each input is read and converted in turn
Why: length becomes 12, width becomes 8 - both ints.
Then compute and show
Why: Verified by execution: 12 * 8 is 96, prints Area: 96.
| variable | value |
|---|---|
| length | 12 |
| width | 8 |
| area | 96 |
Error analysis
Annotate
Walk the callouts on Two inputs: rectangle area. Each one is a place this is easy to get subtly wrong.
Worked example
The user types 7, then 3:
a = int(input("First: "))
b = int(input("Second: "))
print("Average:", (a + b) / 2)Read both numbers first
Why: a is 7, b is 3.
Average uses / , so the result is a float
Why: Verified by execution: (7 + 3) / 2 is 5.0. Note the parentheses - without them, precedence would divide only b by 2.
| expression | value |
|---|---|
| a + b | 10 |
| (a + b) / 2 | 5.0 |
Section
Part 3
Concept
str() goes the other direction: str(100) is the text "100". You need it to join a number into a message with +.
Worked example
score = 100
print("Score: " + str(score))str(score) makes "100", then + joins
Why: Both sides of + are now text, so they glue into "Score: 100".
Read the output
Why: Verified by execution: Score: 100.
| step | value |
|---|---|
| str(score) | "100" |
| "Score: " + str(score) | "Score: 100" |
Anomaly
Predict first
A student writes this, and it looks reasonable:
Joining a number onto text with + but no str().
It is wrong. Say what breaks — and say it before you turn the page.
Correct: "Score: " is a str and score is an int.
Convert with str() - or just use a comma, or an f-string.
Why: "Score: " is a str and score is an int. Python stops rather than guess.
Trap
Joining a number onto text with + but no str().
score = 100
print("Score: " + score)+ cannot join text and a number
Why: "Score: " is a str and score is an int. Python stops rather than guess.
| expression | result |
|---|---|
| "Score: " + 100 | TypeError: can only concatenate str (not "int") to str |
Convert with str() - or just use a comma, or an f-string.
score = 100
print("Score:", score)
print(f"Score: {score}")Three easy fixes
Why: A comma lets print mix types; an f-string drops the value in; str(score) makes it joinable. Real output: Score: 100 (twice).
| fix | output |
|---|---|
| comma | Score: 100 |
| f-string | Score: 100 |
Two truths and a lie
Sort into buckets
Some of these hold up and some are the exact mistakes this lesson is built to prevent. Sort them.
input("...") shows your message, then pauses and waits for the user to type and press Enter.; The answer is only useful if you keep it: name = input("Your name? ").; The text inside input("...") is an ordinary string - end it with a space so the user's typing does not touch your question.5.; Using int() on text that has a decimal point. The user types 3.5.Concept
Put an f before the quotes and wrap each value in { }. Python drops the value straight into the text - no +, no str().
f"Score: {score}" is the clean, modern way to build a message.
Sorting
Sort into buckets
These are the pieces of Session 3 - Input & Output, out of order. Put each one back under the part of the lesson it belongs to.
Intuition
Read an f-string like a fill-in-the-blank sentence. Everything outside { } is printed as-is; each { } is a blank that Python fills with that value's text.
It converts numbers to text automatically, so you never mix up str and int inside it.
Fill the middle
Fill in the blanks
From An f-string in action — one line has had its right-hand side removed. Put it back.
name = "Sam"
badges = 3
print(f"___ has ___ badges")
Why: name is what everything below it consumes, so the wrong expression here fails later and somewhere else. {name} becomes Sam, {badges} becomes 3 - converted for you.
Worked example
name = "Sam"
badges = 3
print(f"{name} has {badges} badges")Each {...} is replaced by that value's text
Why: {name} becomes Sam, {badges} becomes 3 - converted for you.
Read the output
Why: Verified by execution: Sam has 3 badges.
| piece | fills with |
|---|---|
| {name} | Sam |
| {badges} | 3 |
| whole f-string | Sam has 3 badges |
Concept
The braces can hold an expression, not just a variable: f"{c * 9/5 + 32}" computes then inserts the result.
It keeps the calculation right where it is shown - readable and compact.
Worked example
The user types 100:
c = int(input("Temp in C? "))
print(f"{c}C is {c * 9/5 + 32}F")Line 1: read and convert the temperature
Why: c is the number 100.
Line 2: the second blank computes the formula
Why: Verified by execution: 100 * 9/5 + 32 is 212.0, so it prints 100C is 212.0F.
| blank | fills with |
|---|---|
| {c} | 100 |
| {c * 9/5 + 32} | 212.0 |
Concept
Add :.2f inside the braces to show a number with exactly two decimal places - perfect for money.
f"${price:.2f}" turns 4.5 into $4.50, and 3.14159 into 3.14.
Worked example
print(f"{3.14159:.2f}")
print(f"${4.5:.2f}"):.2f rounds to two decimals for display
Why: 3.14159 shows as 3.14; 4.5 shows as 4.50 (padded to two places).
Read the output
Why: Verified by execution. This only changes how it looks, not the stored value.
| expression | shows |
|---|---|
| {3.14159:.2f} | 3.14 |
| ${4.5:.2f} | $4.50 |
Comparison
Comparison matrix
From Two decimal places: refill the shows column from what you know. The rest of the table is as it appeared.
| expression | shows |
|---|---|
| {3.14159:.2f} | 3.14 |
| ${4.5:.2f} | $4.50 |
Section
Part 4
Concept
Only convert when you need to do math. A name, a color, a yes/no answer - leave those as the text input() gives you.
Converting a name with int() would just crash. Match the conversion to what you actually do with the value.
Explain it to yourself
Discussion prompt
In Echoing a text answer this move is made:
Drop it into a message
Why is that legal? Name the rule or definition it rests on before you read on.
Hint: If you can only say "because that is what you do", the rule is the thing to go and find.
Answer:
Verified by execution: with Alex typed, prints Alex is a great choice!
Worked example
The user types Alex:
color = input("Favorite color? ")
print(f"{color} is a great choice!")No conversion - color stays text
Why: We only display it, so the str from input() is exactly what we want.
Drop it into a message
Why: Verified by execution: with Alex typed, prints Alex is a great choice!
| user types | color | prints |
|---|---|---|
| Alex | "Alex" | Alex is a great choice! |
Concept
You can call input() with empty parentheses - it just waits with no message shown. Usually you print a question on the line before.
Worked example
The user types yes:
print("Ready to start?")
answer = input()
print("You said:", answer)The print asks, the bare input() waits
Why: input() with nothing inside still returns whatever is typed.
Echo the answer
Why: Verified by execution: with yes typed, prints You said: yes.
| user types | answer | prints |
|---|---|---|
| yes | "yes" | You said: yes |
Concept
int(input(...)) does it in one line, but you can also read first and convert after: raw = input(...) then age = int(raw).
Two steps can be clearer when you want to check or clean the text before converting.
Step zero
Discussion prompt
Read, then convert — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.
Hint: It starts with: Line 1: raw is the text "25"
Answer:
Worked example
The user types 25:
raw = input("Age? ")
age = int(raw)
print(age + 1)Line 1: raw is the text "25"
Why: Straight from input(), still a str.
Line 2: convert it to a number
Why: int(raw) makes 25. Now math is allowed.
Line 3: the math runs
Why: Verified by execution: 25 + 1 is 26.
| variable | value | type |
|---|---|---|
| raw | "25" | str |
| age | 25 | int |
Intuition
Think of conversion as happening at the door: text comes in from the keyboard, and you turn it into a number the moment it enters.
After that, the rest of your program works with clean numbers and never worries about the string again.
Concept
You can reuse the variable when converting: age = int(age). The right side runs first (text -> number), then the label moves to the number.
After this line, age is a number - the text version is gone, which is usually exactly what you want.
Explain it
Discussion prompt
Explain Overwriting the same name is fine to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.
Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.
Answer:
You can reuse the variable when converting: age = int(age). The right side runs first (text -> number), then the label moves to the number.
Fill the middle
Fill in the blanks
From Convert onto the same name — one line has had its right-hand side removed. Put it back.
age = input("Age? ")
age = int(age)
print(type(age))
Why: age is what everything below it consumes, so the wrong expression here fails later and somewhere else. Verified by execution: type is now <class 'int'>.
Worked example
The user types 7:
age = input("Age? ")
age = int(age)
print(type(age))Line 1: age starts as text
Why: age is "7".
Line 2: int(age) replaces it with a number
Why: Verified by execution: type is now <class 'int'>.
| after line | age | type |
|---|---|---|
| 1 | "7" | str |
| 2 | 7 | int |
Blank canvas
Draw it
Draw what Convert onto the same name just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.
Concept
A program often reads some text answers and some numeric ones. Keep the text as-is and convert only the numbers.
An f-string then combines them in one clean message.
Analogy
Discussion prompt
Explain Mixing a text and a number answer by analogy to something with no Python Fundamentals in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.
Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.
Answer:
A program often reads some text answers and some numeric ones. Keep the text as-is and convert only the numbers.
Fill the middle
Fill in the blanks
From Name plus score — one line has had its right-hand side removed. Put it back.
name = input("Name: ")
score = int(input("Score: "))
print(f"Hi ___, you scored ___ - ___ to go!")
Why: name is what everything below it consumes, so the wrong expression here fails later and somewhere else. name is "Alex"; score is 90 so we can subtract with it.
Worked example
The user types Alex, then 90:
name = input("Name: ")
score = int(input("Score: "))
print(f"Hi {name}, you scored {score} - {100 - score} to go!")name stays text; score is converted
Why: name is "Alex"; score is 90 so we can subtract with it.
One f-string combines both, plus a computed value
Why: Verified by execution: Hi Alex, you scored 90 - 10 to go!
| blank | fills with |
|---|---|
| {name} | Alex |
| {score} | 90 |
| {100 - score} | 10 |
Pattern
Step through it
Step through Name plus score one row at a time. What is driving the change, and what would the row after the last one be?
Section
Part 5
Concept
A tidy program has three phases: gather the inputs, compute the results, then show the output.
Keeping them in that order makes programs easy to read and to debug.
Counterexample
Discussion prompt
A tidy program has three phases: gather the inputs, compute the results, then show the output.
That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.
Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.
Answer:
Keeping them in that order makes programs easy to read and to debug.
Ranking
Put in order
Put the moves of Build it: a trainer profile into the order they have to happen.
Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. Verified by execution: Sam has 3 badges, 5 to go.
Worked example
The user types Sam, then 3:
name = input("Name: ")
badges = int(input("Badges: "))
left = 8 - badges
print(f"{name} has {badges} badges, {left} to go")Gather: one text input, one number input
Why: name is "Sam", badges is 3 (converted).
Compute: badges remaining
Why: 8 - 3 is 5, stored in left.
Show: one f-string ties it together
Why: Verified by execution: Sam has 3 badges, 5 to go.
| variable | value |
|---|---|
| name | "Sam" |
| badges | 3 |
| left | 5 |
Pattern
Step through it
Step through Build it: a trainer profile one row at a time. What is driving the change, and what would the row after the last one be?
Ranking
Put in order
Put the moves of Build it: a tip calculator into the order they have to happen.
Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. Money can have decimals, so float() is the right choice.
Worked example
The user types 40 for the bill:
bill = float(input("Bill: "))
tip = bill * 0.2
total = bill + tip
print(f"Tip: ${tip:.2f} Total: ${total:.2f}")Read the bill as a float
Why: Money can have decimals, so float() is the right choice. bill is 40.0.
Compute tip and total
Why: tip is 40.0 * 0.2 = 8.0; total is 48.0.
Show with money formatting
Why: Verified by execution: Tip: $8.00 Total: $48.00.
| variable | value | shown as |
|---|---|---|
| tip | 8.0 | $8.00 |
| total | 48.0 | $48.00 |
Error analysis
Annotate
Walk the callouts on Build it: a tip calculator. Each one is a place this is easy to get subtly wrong.
Pattern
Predict first
The table runs: {a} | 20 · {b} | 22
In Build it: a two-number adder, given the rows so far: what is the next one — the row where blank is {a + b}?
Correct: {a + b} | 42
| blank | fills with |
|---|---|
| {a} | 20 |
| {b} | 22 |
| {a + b} | 42 |
Why: The relationship between the columns, not the individual numbers, is what generates the next row. a is 20, b is 22. Without int(), the next line would join text instead of add.
Worked example
The user types 20, then 22:
a = int(input("First: "))
b = int(input("Second: "))
print(f"{a} + {b} = {a + b}")Convert both inputs to int
Why: a is 20, b is 22. Without int(), the next line would join text instead of add.
The f-string shows the sum
Why: Verified by execution: 20 + 22 = 42.
| blank | fills with |
|---|---|
| {a} | 20 |
| {b} | 22 |
| {a + b} | 42 |
Trade off
Comparison matrix
From Build it: a two-number adder: every row here is a choice with a cost. Fill the fills with column, then say which row you would actually pick and what you give up for it.
| blank | fills with |
|---|---|
| {a} | 20 |
| {b} | 22 |
| {a + b} | 42 |
Pattern
1. Ask with input() and store the answer
Why: answer = input("..."). The program pauses until Enter.
2. Need a number? Wrap it in int() or float()
Why: int() for whole values, float() for possible decimals. Skip this and any math is a TypeError.
3. Compute the results
Why: Do the arithmetic on the converted numbers, storing what you need.
4. Respond with an f-string
Why: f"{a} + {b} = {a + b}" drops values (and even expressions) straight into the message. Use :.2f for money.
Pattern
Whole count (age, score, badges) -> int()
Why: int(input(...)). Rejects decimal text, so only use it when a dot is impossible.
Possible decimal (money, temp, average) -> float()
Why: float(input(...)). Accepts both "3.5" and "4".
Just displaying it -> leave it as text
Why: A name or a yes/no answer needs no conversion at all.
Real world
Discussion prompt
Outside this lesson: where does Session 3 - Input & Output actually turn up? Name one concrete situation — a job, a piece of software someone ships, a decision somebody has to make — and say which part of Choosing the right conversion is doing the work in it.
Hint: Vague is the failure mode here. "Engineering" is not a situation; "deciding whether this build is fast enough to ship" is.
Answer:
Session 3 of the Python Fundamentals series, in depth. Making the program listen with input() and talk back with print().
Section
Part 5
Check
The user types 20.
score = input("Score? ")
print(type(score))| user types | type(score) |
|---|---|
| 20 | ? |
Check your understanding
What does type(score) print?
Answer: B
Why: input() always returns a str, even for digits. score is the text "20", so type(score) is <class 'str'>. Verified by execution.
Check
The user types 4.
n = input("A number: ")
print(n + 2)| n | n + 2 |
|---|---|
| "4" | ? |
Check your understanding
What happens?
Answer: C
Why: n is the text "4" because input() returns a str. Adding the number 2 mixes a str and an int, so Python stops. int(input(...)) would make it print 6. Verified by execution.
Check
The user types 4. Which line makes it print 6?
n = int(input("A number: "))
print(n + 2)| n | n + 2 |
|---|---|
| 4 (int) | ? |
Check your understanding
Now what prints?
Answer: A
Why: int(input(...)) converts "4" to the number 4, so 4 + 2 is 6. Wrapping input in int() is exactly what enables the math. Verified by execution.
Check
The user types 2.5.
x = int(input("Value: "))| input | result |
|---|---|
| 2.5 | ? |
Check your understanding
What happens?
Answer: C
Why: int() only parses whole-number text. "2.5" has a decimal point, so int() refuses and raises a ValueError. Use float() for decimals. Verified by execution.
Check
What fills the braces?
a = 6
b = 4
print(f"{a} and {b} make {a + b}")| blank | fills with |
|---|---|
| {a} | ? |
| {a + b} | ? |
Check your understanding
What does this print?
Answer: A
Why: The f before the quote makes each {...} a blank filled with that value's text: 6, 4, and the computed 6 + 4 = 10. Verified by execution.
Comparison
Comparison matrix
From Check: f-string blanks: refill the fills with column from what you know. The rest of the table is as it appeared.
| blank | fills with |
|---|---|
| {a} | ? |
| {a + b} | ? |
Check
What does :.2f do here?
price = 5
print(f"${price:.2f}")| value | shown as |
|---|---|
| 5 | ? |
Check your understanding
What does this print?
Answer: A
Why: :.2f formats the number with exactly two decimal places, so 5 shows as 5.00. The value is unchanged - only its display. Verified by execution.
Check
You are reading a bill amount that could be $19.99.
bill = ____(input("Bill? "))| needs | so use |
|---|---|
| decimals allowed | ? |
Check your understanding
What should fill the blank?
Answer: A
Why: A bill like 19.99 has a decimal point, so float() is the right converter. int() would raise a ValueError on "19.99". Verified by reasoning about int()'s decimal restriction.
Connect it up
Draw it
One page, no notation unless you need it: draw how these connect — Asking with input() · Converting the Answer · Building Output · Text Answers & Converting Later · Putting It Together · Check Yourself. Put an arrow wherever one of them is what makes another possible, and label the arrow with why.
Recap
input() asks and returns the answer as text. To do math, convert with int() or float(). Respond with an f-string.
| You write | You get |
|---|---|
| input("Age? ") | text, e.g. "12" |
| int(input("Age? ")) | a number, e.g. 12 |
| float(input("Bill? ")) | a decimal, e.g. 19.99 |
| f"{a} + {b} = {a + b}" | drops values and math into text |
| f"${x:.2f}" | two decimal places for money |
The program can now listen and reply. Next session it learns to decide - to do different things based on the answer, with if / elif / else.
Want this taught 1-on-1? Alexander tutors Python Fundamentals — $55/session, free consultation.