11-15-2025, 10:24 PM
Chapter 5 — Input & Output
Your programs can now talk to the user
Up until now, your programs have only displayed information.
In this chapter, you will learn how to:
• ask the user for information
• store what they type
• respond to them
• create interactive programs
Input + output is the foundation of almost every real application.
---
5.1 Output: print()
You already know print():
But now we go one step further.
Output becomes powerful when combined with input.
---
5.2 Input: Asking the User Questions
To get information from the user, use:
Example:
If the user types:
The output is:
---
5.3 Input Always Gives You a String
Important:
Whatever the user types comes in as TEXT (a string).
Example:
Python sees “16” as text, not a number.
To convert to a number:
Now it works.
---
5.4 Converting User Input
Use:
• int() → convert to whole number
• float() → convert to decimal
• str() → convert to text
Examples:
---
5.5 Combining Input + Output
Try this:
Your program now behaves like a little conversation.
---
5.6 A Simple Calculator Program
This program asks for two numbers and adds them:
This is your first interactive tool.
---
5.7 Building a Custom Greeting
Try this:
This combines variables, input, and arithmetic.
---
5.8 Avoiding Common Input Mistakes
Common mistake:
If the user enters 3, the output is:
Because Python repeats the string.
You MUST convert:
---
5.9 Exercise — Build a Personal Greeting App
Create a file called greet.py.
Ask the user for:
• their name
• their age
• their city
Then print a message like:
Try to format it neatly.
---
5.10 Small Challenge
Make a program that asks the user for:
• a price
• a discount percentage
Then calculate the discounted price and display it.
Hint:
---
5.11 Chapter Summary
• input() asks the user a question
• input ALWAYS returns a string
• use int() or float() to convert numbers
• combine input and output for interactive programs
• print() displays results and responses
• you can now build your first simple tools
Next:
Chapter 6 — Strings
This is where you learn how to manipulate text — a crucial skill for all beginners.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
Your programs can now talk to the user
Up until now, your programs have only displayed information.
In this chapter, you will learn how to:
• ask the user for information
• store what they type
• respond to them
• create interactive programs
Input + output is the foundation of almost every real application.
---
5.1 Output: print()
You already know print():
Code:
print("Hello!")But now we go one step further.
Output becomes powerful when combined with input.
---
5.2 Input: Asking the User Questions
To get information from the user, use:
Code:
input("Your question here: ")Example:
Code:
name = input("What is your name? ")
print("Hello,", name)If the user types:
Code:
MiaThe output is:
Code:
Hello, Mia---
5.3 Input Always Gives You a String
Important:
Whatever the user types comes in as TEXT (a string).
Example:
Code:
age = input("How old are you? ")
print(age + 1) # ERROR!Python sees “16” as text, not a number.
To convert to a number:
Code:
age = int(input("How old are you? "))
print(age + 1)Now it works.
---
5.4 Converting User Input
Use:
• int() → convert to whole number
• float() → convert to decimal
• str() → convert to text
Examples:
Code:
x = int(input("Enter a number: "))
y = float(input("Enter a decimal: "))---
5.5 Combining Input + Output
Try this:
Code:
name = input("What is your name? ")
food = input("What is your favourite food? ")
print("Nice to meet you,", name)
print("I also like", food)Your program now behaves like a little conversation.
---
5.6 A Simple Calculator Program
This program asks for two numbers and adds them:
Code:
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
total = a + b
print("The total is:", total)This is your first interactive tool.
---
5.7 Building a Custom Greeting
Try this:
Code:
name = input("Your name: ")
age = int(input("Your age: "))
print("Hello", name)
print("Next year you will be", age + 1)This combines variables, input, and arithmetic.
---
5.8 Avoiding Common Input Mistakes
Common mistake:
Code:
number = input("Enter a number: ")
print(number * 5)If the user enters 3, the output is:
Code:
33333Because Python repeats the string.
You MUST convert:
Code:
number = int(input("Enter a number: "))
print(number * 5)---
5.9 Exercise — Build a Personal Greeting App
Create a file called greet.py.
Ask the user for:
• their name
• their age
• their city
Then print a message like:
Code:
Hello Mia! You are 16 from Liverpool.Try to format it neatly.
---
5.10 Small Challenge
Make a program that asks the user for:
• a price
• a discount percentage
Then calculate the discounted price and display it.
Hint:
Code:
discount = price * (percent / 100)
final_price = price - discount---
5.11 Chapter Summary
• input() asks the user a question
• input ALWAYS returns a string
• use int() or float() to convert numbers
• combine input and output for interactive programs
• print() displays results and responses
• you can now build your first simple tools
Next:
Chapter 6 — Strings
This is where you learn how to manipulate text — a crucial skill for all beginners.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
