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.
The Input
keyword lets your program ask questions and wait for the user to provide answers.
var variableName = Input "Your question here?"
Algorithm SimpleInput
var userName = Input "What is your name?"
Print "Hello,", userName, "!"
Endalgorithm
You've been using Print
already, but let's explore all its capabilities!
Print "Hello, World!"
var name = "Alice"
Print name
Print "Name:", name, "- Age:", age
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
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
When you get input, you can use it in calculations:
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
โ Bad: Input "?"
โ
Good: Input "Enter your age in years:"
Clear prompts help users understand what information you need.
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
Make your program feel conversational and helpful:
Print "Welcome to the program!"
Print "Please enter your information:"
Print ""
# ... get input ...
Print ""
Print "Thank you!"
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
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
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 "" |
Input
to get data from usersPrint
to display text and variablesPrint ""
) to improve readability