Lesson 4 โ€ข Beginner

Input and Output

15 minutes
Foundation

๐ŸŽค Making Programs Interactive

So far, our programs have been static - they do the same thing every time. But real programs need to interact with users! In this lesson, you'll learn how to get input from users and display customized output.

Visual: Input โ†’ Process โ†’ Output Flow

๐Ÿ“ฅ
INPUT
Get Data
User enters: "Alice"
โ†’
โš™๏ธ
PROCESS
Work with Data
Store in: userName
โ†’
๐Ÿ“ค
OUTPUT
Display Result
Show: "Hello, Alice!"
Why Interactive Programs?
Interactive programs can adapt to different situations and users. They can ask questions, receive answers, process data, and provide personalized results - making them much more useful!

๐Ÿ“ฅ Getting User Input

The Input keyword lets your program ask questions and wait for the user to provide answers.

Basic Input Syntax

var variableName = Input "Your question here?"
Example: Simple Input
Algorithm SimpleInput

var userName = Input "What is your name?"
Print "Hello,", userName, "!"

Endalgorithm
Example Session
What is your name? Alice Hello, Alice !

How Input Works

  1. Program displays the prompt message
  2. Program pauses and waits for user input
  3. User types their response and presses Enter
  4. Value is stored in the variable
  5. Program continues execution

๐Ÿ“ค Displaying Output

You've been using Print already, but let's explore all its capabilities!

Printing Text

Print "Hello, World!"

Printing Variables

var name = "Alice"
Print name

Printing Multiple Items

Print "Name:", name, "-  Age:", age
Example: Various Print Styles
Algorithm PrintExamples

var name = "Bob"
var age = 25
var city = "Paris"

# Different ways to print
Print name
Print "Age:", age
Print name, "lives in", city
Print ""  # Empty line
Print name, "is", age, "years old"

Endalgorithm
Output
Bob Age: 25 Bob lives in Paris Bob is 25 years old

๐Ÿ”„ Interactive Program Example

Complete Example: Personal Greeting
Algorithm PersonalGreeting

# Get user information
var name = Input "What is your name?"
var age = Input "How old are you?"
var hobby = Input "What is your favorite hobby?"

# Display personalized message
Print ""
Print "=============================="
Print "    PERSONAL PROFILE"
Print "=============================="
Print ""
Print "Hello,", name, "!"
Print "You are", age, "years old."
Print "You enjoy", hobby, "!"
Print ""
Print "Nice to meet you!"

Endalgorithm
Example Session
What is your name? Alice How old are you? 22 What is your favorite hobby? painting ============================== PERSONAL PROFILE ============================== Hello, Alice ! You are 22 years old. You enjoy painting ! Nice to meet you!

Working with Numbers

When you get input, you can use it in calculations:

Example: Simple Calculator
Algorithm SimpleCalculator

Print "=== Simple Calculator ==="
Print ""

# Get numbers from user
var num1 = Input "Enter first number:"
var num2 = Input "Enter second number:"

# Perform calculations
var sum = num1 + num2
var difference = num1 - num2
var product = num1 * num2
var quotient = num1 / num2

# Display results
Print ""
Print "Results:"
Print "---------"
Print num1, "+", num2, "=", sum
Print num1, "-", num2, "=", difference
Print num1, "ร—", num2, "=", product
Print num1, "รท", num2, "=", quotient

Endalgorithm

Tips for Good Input/Output

1. Clear Prompts

Use Descriptive Prompts

โŒ Bad: Input "?"

โœ… Good: Input "Enter your age in years:"

Clear prompts help users understand what information you need.

2. Formatted Output

Make your output easy to read with labels and spacing:

# Add spacing and labels
Print ""  # Empty line for spacing
Print "Name:", name  # Label the data
Print "---"  # Visual separators

3. User-Friendly Messages

Make your program feel conversational and helpful:

Print "Welcome to the program!"
Print "Please enter your information:"
Print ""
# ... get input ...
Print ""
Print "Thank you!"

Real-World Examples

Example: Shopping Total Calculator
Algorithm ShoppingCalculator

Print "=== SHOPPING CALCULATOR ==="
Print ""

# Get product information
var productName = Input "Product name:"
var price = Input "Price per item:"
var quantity = Input "Quantity:"

# Calculate total
var subtotal = price * quantity
var tax = subtotal * 0.08  # 8% tax
var total = subtotal + tax

# Display receipt
Print ""
Print "==============================="
Print "         RECEIPT"
Print "==============================="
Print "Product:", productName
Print "Price:", price
Print "Quantity:", quantity
Print "-------------------------------"
Print "Subtotal: $", subtotal
Print "Tax (8%): $", tax
Print "==============================="
Print "TOTAL: $", total
Print "==============================="

Endalgorithm
Example: Temperature Converter
Algorithm TempConverter

Print "=== Temperature Converter ==="
Print "(Celsius to Fahrenheit)"
Print ""

# Get temperature from user
var celsius = Input "Enter temperature in Celsius:"

# Convert: F = C ร— 9/5 + 32
var fahrenheit = celsius * 9 / 5 + 32

# Display result
Print ""
Print celsius, "ยฐC =", fahrenheit, "ยฐF"

Endalgorithm

๐Ÿ“‹ Quick Reference

Operation Syntax Example
Get Input var x = Input "prompt" var name = Input "Name?"
Print Text Print "text" Print "Hello"
Print Variable Print variable Print name
Print Multiple Print item1, item2 Print "Age:", age
Empty Line Print "" Print ""

Key Takeaways

  • โœ… Use Input to get data from users
  • โœ… Use Print to display text and variables
  • โœ… You can print multiple items separated by commas
  • โœ… Input values can be used in calculations
  • โœ… Clear prompts and formatted output make programs user-friendly
  • โœ… Use empty lines (Print "") to improve readability

๐Ÿ’ช Practice Exercises

Try These Exercises

Exercise 1: Personal Info Card
Create a program that asks for name, age, email, and phone number, then displays them in a nicely formatted card.
Exercise 2: Distance Converter
Get a distance in kilometers from the user and convert it to miles (1 km = 0.621371 miles). Display both values.
Exercise 3: Circle Calculator
Ask for a circle's radius, then calculate and display both the area (ฯ€ ร— rยฒ) and circumference (2 ร— ฯ€ ร— r).
Exercise 4: Tip Calculator
Get a restaurant bill amount, calculate 15% tip and 20% tip, then display both options with the total for each.
Excellent Work!
You can now create interactive programs that communicate with users! In the next lesson, we'll explore operators to perform more complex calculations and comparisons.