Lesson 3 of 8

Input and Output

Learn how to get information from users and display results

Learning Objectives

  • Use the input() function to get information from users
  • Format output with print() in different ways
  • Create interactive programs that respond to user input
  • Build a simple conversation program

Getting Input from Users

The input() function lets your program ask questions and wait for the user to type an answer. It's like having a conversation with your computer!

Your First Interactive Program:

# Ask the user for their name
name = input("What is your name? ")
print("Hello,", name, "!")

# Ask for their age
age = input("How old are you? ")
print("Wow,", age, "is a great age!")

Try it! Type this code in Spyder and run it. The program will wait for you to type your answers!

Creative Ways to Display Information

There are many ways to make your output look nice and organized:

Basic Printing

name = "Alex"
age = 12

print("Name:", name)
print("Age:", age)
print("Nice to meet you!", name)

F-strings (Cool!)

name = "Alex"
age = 12

print(f"Hi {name}!")
print(f"You are {age} years old")
print(f"Next year you'll be {age + 1}!")

Making Pretty Output

# Create a nice-looking profile
name = input("Enter your name: ")
hobby = input("What's your favorite hobby? ")
color = input("What's your favorite color? ")

print("=" * 30)
print(f"    PROFILE: {name}")
print("=" * 30)
print(f"Hobby: {hobby}")
print(f"Color: {color}")
print("=" * 30)

Getting Numbers from Users

When you use input(), Python treats everything as text. To do math, you need to convert it to numbers:

This Won't Work as Expected:

num1 = input("Enter first number: ")  # User types "5"
num2 = input("Enter second number: ") # User types "3"
result = num1 + num2                  # This gives "53", not 8!
print(f"The answer is: {result}")

The Right Way:

num1 = int(input("Enter first number: "))  # Convert to integer
num2 = int(input("Enter second number: ")) # Convert to integer
result = num1 + num2                       # Now this gives 8!
print(f"Result: {result}")

Practice Exercise

Build a Simple Calculator

Create a program that asks for two numbers and shows their sum:

# Simple Calculator
print("=== SIMPLE CALCULATOR ===")
print()

# Get two numbers from the user
first_number = int(input("Enter the first number: "))
second_number = int(input("Enter the second number: "))

# Calculate the sum
total = first_number + second_number
print(f"The answer is {total}!")
print()
print(f"{first_number} + {second_number} = {total}")
print("Thanks for using the calculator!")

Fun Challenge

Create a Chatbot

Make a simple chatbot that asks questions and responds:

# Simple Chatbot
print("🤖 Hello! I'm a friendly chatbot!")
print()

name = input("What's your name? ")
print(f"Nice to meet you, {name}!")

age = int(input("How old are you? "))
if age < 13:
    print(f"You picked {age}! That's a great choice.")
elif age < 20:
    print("You're a teenager! Cool!")
else:
    print("You're an adult! Nice!")

favorite_animal = input("What's your favorite animal? ")
print(f"I love {favorite_animal}s too!")

Challenge: Can you add more questions to make the chatbot even more interesting?

Tips and Tricks

Making Input Clear

# Good: Clear instructions
name = input("Please enter your name: ")

# Better: Even clearer
name = input("What should I call you? ")

Adding Style

# Use emojis and symbols
print("🎉 Welcome to my program! 🎉")
print("-" * 25)
print("✨ Let's get started! ✨")