Table of Contents
Input and output are essential components of any interactive Python program. Whether you’re creating a quiz, calculator, or simple data collection tool, you’ll need to gather
input from the user and display results. In Python, input()
allows users to provide data, while print()
displays it back in a readable format. At the
beginner level, mastering I/O teaches you how to structure your code, capture user input, perform type conversions, and present clean, informative output. These are the first
steps to making your programs dynamic and user-friendly.
To complete these tasks, you should already be familiar with:
- Basic Python syntax
- Using variables
- Writing comments
- Working with strings
- Input and Output
- String concatenation and type conversion with
int()
orfloat()
Beginner Level Practice – Input and Output
This beginner section is focused on applying input()
and print()
in small, real-life inspired programs. You will collect user input, store it in
variables, process it slightly, and display personalized messages. These exercises build confidence in handling string input, converting data types, and writing readable output.
They also help you get used to the basic flow of asking for data, performing a calculation, and returning feedback to the user. After completing these tasks, you'll be more
comfortable writing programs that interact with people in meaningful ways.
Task 1: Ask the User’s Age and Calculate the Next Birthday
Write a program that asks the user to enter their age. Then, use that input to calculate how old they will be next year and display a message like: “Next year you will be 26 years old.” This task helps simulate a basic age calculator and reinforces simple numeric input and output formatting.
What this Task Teaches
This task develops your understanding of how to get numeric input using input()
and convert it using int()
. You’ll also practice storing values in
variables and formatting the output message in a user-friendly way. This builds your ability to turn static text into dynamic, responsive output.
Hints and Tips
- Use
input()
to receive a string from the user. - Use
int()
to convert it to an integer. - Add 1 to the number and use
print()
to display the message. - Use commas or
f-strings
to format the message.
Common Mistakes
-
Forgetting to convert input to an integer:
input()
always returns a string. If you try to add 1 to it without conversion, you’ll get aTypeError
. Always useint()
orfloat()
when working with numbers. -
Using
+
for numeric and string concatenation: Trying to mix strings and numbers with+
can cause errors. Usef-strings
or commas insideprint()
to combine them. - Invalid input values: If the user types a word instead of a number,
int()
will crash. You’ll handle that later with error checking. -
Not spacing output properly: Beginners often forget to add spaces in the final printed message, leading to results like
“Next year you will be25years old”
. - Using age as a string throughout: Always convert once and use the numeric version in calculations.
Step-by-Step Thinking
Think through how a user interacts with your program: they type their age, and the system replies with a result. Your job is to capture, process, and return this data.
- Ask the user to input their age using
input()
- Store the result in a variable, e.g.,
age
- Convert the value to an integer using
int()
- Add 1 to the value to represent next year
- Use
print()
to format and display the message
How to Make it Harder
- Ask the user for both name and age, and include both in the final output.
- Format the sentence using
f-strings
orstr.format()
. - Add validation: if the age is not a number, show a warning.
Task 2: Collect and Display a Personalized Message
Write a Python program that asks the user for their name and favorite color. Then display a sentence like: “Hello, Alice! Your favorite color is blue.” This kind of interaction is useful in quizzes, surveys, or welcome messages in applications.
What this Task Teaches
This exercise reinforces how to work with multiple user inputs and store them in separate variables. You’ll also focus on formatting output for clarity and friendliness. It also helps beginners practice clean string concatenation and sequencing of inputs.
Hints and Tips
- Use
input()
twice: once for the name, once for the color. - Store each value in a clearly named variable.
- Use
print()
with commas orf-strings
to output the sentence. - Capitalize the first letters for aesthetics (optional).
Common Mistakes
- Reusing the same variable for both inputs: Make sure you use one variable for the name and a separate one for the color. Otherwise, you’ll overwrite the first input.
- Incorrect string formatting: Beginners often forget to add spaces or punctuation in the output, resulting in awkward or unreadable sentences.
-
Inconsistent casing: If your message uses lowercase names but capitalized greetings, it can look unprofessional. You can optionally use
.capitalize()
to clean inputs. - Forgetting to test output: Always check what your program prints before finalizing it.
- Hardcoding values instead of using variables: Don’t write
"Hello, Alice!"
; use thename
variable instead.
Step-by-Step Thinking
Think about what a user would expect: they provide their name and color, and see a clear message back. Keep it conversational and accurate.
- Use
input()
to ask for the name and save toname
- Use
input()
again to ask for the favorite color and save tocolor
- Format the output message using
print()
- Insert both variables into the sentence
- Test with different names and colors
How to Make it Harder
- Capitalize both inputs using
.capitalize()
before displaying them. - Ask for three inputs: name, color, and age - and use them all in one sentence.
- Add emoji or symbols to make the message friendlier.
Intermediate Level Practice – Input and Output
At the intermediate level, input and output operations become more dynamic. You’ll not only read simple values from users, but also format responses, process numerical input, and
build logic-driven programs. Tasks at this level mimic real-life tools - like tip calculators or simple scheduling systems - where the flow depends on the user’s answers. This is
your chance to apply input()
and print()
with real computation, conditional logic, and string formatting. You’ll also start thinking about the user
experience: how clear is your prompt, how readable is the output?
Task 1: Simple Tip Calculator
Write a program that asks the user for the total bill amount and the desired tip percentage. It should then calculate and display how much tip to leave. For example, if the
user enters 100
as the bill and 15
as the tip percent, the program should print: “Leave $15.00 as a tip.”
What this Task Teaches
This task teaches you to process numeric inputs, perform arithmetic with floating-point numbers, and format output with two decimal places. It also helps you write clear prompts and accurate feedback, preparing you to build interactive, user-facing tools.
Hints and Tips
- Convert inputs to
float
for decimal calculations. - Multiply the bill by the percentage divided by 100.
- Use
round()
or string formatting for two decimal places. - Be sure your prompts clearly ask for a number, not a string.
Common Mistakes
-
Forgetting to convert input to float: If you leave the input as a string, trying to multiply it will result in an error. Always use
float()
before arithmetic. - Incorrect percentage formula: Some beginners mistakenly divide by 100 twice or forget it altogether. Remember: percentage = value × (percent / 100).
- Too many or too few decimal places: Output like
15.0000001
looks messy. Use rounding or formatted strings for readability. - Hardcoded values: Don’t use fixed numbers like 0.15 - ask the user for the percentage so they can choose.
- Unclear prompts: Always label your inputs (e.g., “Enter bill amount in dollars”) so users know what to do.
Step-by-Step Thinking
You’re building a tool used by people in real life. Think about the flow: get inputs, process them, and return clear, friendly output.
- Ask the user for the bill total using
input()
- Convert the input to
float
and store it - Ask the user for the desired tip percentage
- Convert that input to
float
as well - Calculate the tip as
bill × (percent / 100)
- Format the result to two decimal places
- Print a message showing the calculated tip
How to Make it Harder
- Also display the total bill including the tip.
- Allow the user to choose between tip options (10%, 15%, 20%).
- Add input validation for non-numeric entries.
Task 2: Time Left Until Bedtime
Ask the user what time it is now (in hours, 24-hour format) and what time they want to go to sleep. Then calculate how many hours are left until bedtime. For example, if it’s 18:00 now and bedtime is 23:00, print: “You have 5 hours left before sleep.”
What this Task Teaches
This task teaches you to handle time-based logic, perform subtraction, and handle special cases like when bedtime is after midnight. It also strengthens your arithmetic thinking using user inputs in a more contextual scenario.
Hints and Tips
- Use
int()
orfloat()
to work with hour values. - If bedtime is less than current time, add 24 to bedtime before subtracting.
- Keep your message friendly and use proper time units.
Common Mistakes
- Skipping time conversion: If bedtime is 1 and current time is 22, a direct subtraction gives a negative number. Use 24-hour logic by adjusting bedtime when needed.
- Incorrect hour format: Beginners sometimes mix formats - like using “8 PM” instead of 20. Be clear that the user must enter numbers from 0 to 23.
- Wrong subtraction order: Subtracting
current - bedtime
gives a negative value. The correct formula isbedtime - current
. - Assuming bedtime is always after now: Some people go to sleep after midnight. Handle that with a 24-hour wrap-around.
- No type conversion: Don’t try to subtract strings - convert to
int
orfloat
first.
Step-by-Step Thinking
Imagine a user checking how long until sleep - your program should do the math and return a clean message. Consider edge cases around midnight.
- Ask the user for the current hour using
input()
- Convert the value to
int
and store it ascurrent_hour
- Ask for the bedtime hour and store it as
bedtime_hour
- If bedtime is less than or equal to current, add 24 to bedtime
- Subtract current from bedtime
- Display the number of hours left with a friendly message
How to Make it Harder
- Ask for both hour and minute, and calculate total minutes left.
- Account for minutes using
datetime
or manual calculations. - Give a recommendation: “It’s getting late! Consider sleeping now.”
Advanced Level Practice – Input and Output
At the advanced level, input and output tasks evolve into mini applications. You’ll need to design clear input flows, handle edge cases, apply conditional logic, and ensure readable, user-friendly output. These exercises combine I/O with core Python structures like loops, conditionals, and data formatting. You’ll simulate realistic systems - like password prompts or budget checkers - where communication with the user is at the center of the program. Precision, clarity, and robustness are key at this level.
Task 1: Multi-Step Login System
Create a login prompt that first asks for a username, then for a password. If both are correct (you can hardcode them, e.g., “admin” and “secret123”), print a success message. If either is wrong, print an error. Optionally, allow the user up to 3 attempts and show how many attempts remain.
What this Task Teaches
You’ll learn how to use multiple input()
calls in a logical sequence, apply conditional checks, use loops for retry attempts, and display precise output messages
depending on user input. It’s a foundation for authentication logic in real-world software.
Hints and Tips
- Use two variables to store correct credentials.
- Use a
while
loop to limit attempts. - Compare inputs with the correct values using
==
. - Display different messages for incorrect username, password, or both.
Common Mistakes
-
Using
=
instead of==
in comparisons: This leads to an assignment error or incorrect behavior. Always use==
when checking input. - Hardcoding both checks together: Beginners often check both username and password in one line, making it hard to isolate what failed. Separate the logic for better feedback.
- Allowing unlimited attempts: Without a counter or loop condition, users can retry forever - always control attempts.
- Incorrect string comparison due to whitespace: Users may accidentally type spaces. Use
.strip()
to sanitize input. - No feedback after failure: A generic "Invalid" message isn’t helpful. Tell the user what went wrong and how many tries are left.
Step-by-Step Thinking
Think like a security system. You must ask, validate, respond - and track user behavior with conditions. Structure your logic for clear user communication.
- Define variables for correct username and password
- Use a loop that repeats up to 3 times
- Ask the user for both username and password using
input()
- Check both credentials using
==
- If correct, display success and
break
the loop - If incorrect, inform the user and reduce attempts count
- After 3 failed tries, show a lockout message
How to Make it Harder
- Store credentials in a dictionary and support multiple users.
- Hide password input using a placeholder (e.g.,
getpass
module). - Log failed attempts with timestamps.
Task 2: Monthly Budget Evaluator
Build a script that asks the user for their monthly income and expenses across three categories: rent, food, and other. Then calculate the total expense and display whether they are under or over budget. Print a message like: “You are $150 over your budget!” or “You have $120 left this month.”
What this Task Teaches
This exercise involves collecting multiple numeric inputs, performing aggregation, and presenting conditional results. You’ll learn how to guide the user through a step-by-step input flow and summarize their financial data clearly.
Hints and Tips
- Convert all inputs using
float()
. - Sum the expenses to get a total.
- Compare total expenses with income to determine the result.
- Use
abs()
andround()
for clean values in output.
Common Mistakes
- Not converting inputs: If you forget
float()
, you’ll be trying to do math with strings, leading to type errors or incorrect results. - Wrong order of operations: Ensure you calculate total expenses before comparing with income.
- Unclear user prompts: Always tell the user exactly what data to enter (“Monthly rent in dollars:”) to prevent confusion.
-
Negative signs in messages: Beginners often write “You have -$100 left”, which sounds confusing. Format output with
abs()
and proper phrasing. - Not using rounding: Showing too many decimals (e.g., 147.3333333) makes the output unreadable.
Step-by-Step Thinking
Think like a financial tracker: gather clear numbers, sum them, and offer readable results that make sense to the user.
- Ask for the monthly income and convert it to
float
- Ask for three expense inputs: rent, food, and other
- Convert each to
float
and store them - Sum all expenses
- Subtract total expenses from income
- Use a condition: if result > 0, show “left” message; else show “over” message
- Use
abs()
to display positive values in the message
How to Make it Harder
- Add more categories (transport, healthcare, entertainment).
- Display a pie chart of expenses using a library like
matplotlib
. - Allow saving the results to a file or displaying spending suggestions.