Education digital-technologies year-9
Chapter 5 - User Input
A lesson on collecting user input, converting types correctly, and using what the user enters.
Chapter 5 shifts the program from static output to interaction by collecting and using user input.
Chapter Navigation
Chapter 5 — Letting the user speak
EXPLORER · v4 · turning typed input into usable data
Where we are now
So far, the console has only known information because you typed it into the program.
That works while you are building it, but it is not very flexible.
If someone else runs your program, they should be able to enter their own mission details.
This chapter is about making that happen properly.
That works while you are building it, but it is not very flexible.
If someone else runs your program, they should be able to enter their own mission details.
This chapter is about making that happen properly.
Asking the user for information
In Python, a program asks the user for information using the
When Python reaches an
Whatever the user types is stored in a variable.
input() function. When Python reaches an
input() line, it pauses, waits for the user to type something, and then continues running. Whatever the user types is stored in a variable.
An important behaviour to understand
Everything that comes from
This is always true.
Even if the user types a number.
Python treats input as text because it cannot safely guess what the user meant.
input() is a string. This is always true.
Even if the user types a number.
Python treats input as text because it cannot safely guess what the user meant.
Turning strings into numbers
If you want to do maths with a value from
Converting means telling Python what type the value should become.
To convert a string into a whole number, use
To convert a string into a decimal number, use
If the text cannot be turned into a number, Python will stop and show an error.
That is not a failure. It is Python protecting your program from bad data.
input(), you must convert it. Converting means telling Python what type the value should become.
To convert a string into a whole number, use
int(). To convert a string into a decimal number, use
float(). If the text cannot be turned into a number, Python will stop and show an error.
That is not a failure. It is Python protecting your program from bad data.
EXPLORER v4 (run this)
This version of the console collects information from the user.
Notice how values are collected first as strings, then converted before being used.
Read the comments carefully. They explain each step.
Notice how values are collected first as strings, then converted before being used.
Read the comments carefully. They explain each step.
"""
EXPLORER v4 — Chapter 5
This version collects mission details from the user.
Input arrives as strings and is converted into the correct data types.
"""
# =========================
# COLLECT MISSION DETAILS
# =========================
# This value is text, so leaving it as a string makes sense
mission_location = input("Enter mission location: ")
# This arrives as a string, even if the user types a number
mission_day_text = input("Enter mission day (whole number): ")
# Convert the string into an int so Python can treat it as a number
mission_day = int(mission_day_text)
# This also arrives as a string
temperature_text = input("Enter temperature (°C): ")
# Convert the string into a float for decimal values
temperature_c = float(temperature_text)
# =========================
# SYSTEM STATE
# =========================
system_online = True
# =========================
# CONSOLE OUTPUT
# =========================
print("=" * 44)
print(" EXPLORER CONSOLE ")
print("=" * 44)
print("Location: " + mission_location)
print("Day: " + str(mission_day))
print("Temperature: " + str(temperature_c) + " °C")
print()
print("System status:")
print("Online: " + str(system_online))
print("=" * 44)
Try this carefully
Run the program and enter valid numbers first.
Then run it again and type a word where a number is expected.
Read the error message Python shows.
Errors are not punishments. They are clues.
Then run it again and type a word where a number is expected.
Read the error message Python shows.
Errors are not punishments. They are clues.
Side note: a cleaner way to print values
Earlier chapters quietly used a Python feature called an f-string.
An f-string is a formatted string. It lets you place values directly inside text.
Long version:
Using an f-string:
Both are correct. The f-string just makes mixed output easier to read once you understand types.
An f-string is a formatted string. It lets you place values directly inside text.
Long version:
print("Day: " + str(mission_day))
Using an f-string:
print(f"Day: {mission_day}")
Both are correct. The f-string just makes mixed output easier to read once you understand types.
Next time
Right now, the console collects information but treats all missions the same.
In the next chapter, we will teach it to make decisions using
In the next chapter, we will teach it to make decisions using
if statements.