Module 2: Input & Output
Output to the Screen
To output text or variables to the user's screen in ERL, we use the print() statement.
print("Hello World!")
score = 100
print(score)
You can combine text and variables using the + operator (called concatenation).
Note: In ERL, we usually cast numbers to strings before concatenation:
name = "Bob"
print("Hello " + name)
print("Your score is " + str(score))
Exercise: Print Statement
Write an ERL statement to print "Learning ERL is fun!".
ex1.erl
Input from the User
To get data from the keyboard, we use input(). We usually store the returned value in a variable.
color = input("What is your favorite color?")
print("I like " + color + " too!")
> Crucial Exam Tip:
input() always returns a **string**. If you ask for a number (like age), you must cast it to an integer or float if you want to do math with it!
ageStr = input("How old are you?")
age = int(ageStr)
print("Next year you will be " + str(age + 1))
Exercise: Getting Input
Write a program that asks the user for their name, and then asks for their birth year. Calculate their approximate age (assume the current year is 2024), and print out a greeting like: "Hello Alice, you are 16 years old."
ex2.erl