Home · Academy · Robotics & Coding · Python Fundamentals · Getting Input from the User

Getting Input from the User

Learn to read input with input(), convert text to numbers and print output with f-strings.

LESSON COMPASS

What will you use this page for?

Core idea

The input() function lets a program ask the user a question while it is running and store the answer typed on the keyboard in a variable.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Forgetting to use int() This is the most common mistake. Because input() always gives text, if you do not convert it with int() before doing number operations, you will either get an error or an unexpected result (text stuck together). number = input("Number: ") # number is text number = int(input("Number: ")) #…

Next connection

Conditions: With if , elif , and else we learn how a program can make different decisions based on the user's answer.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteVariables and Data Types
ContentStandard lesson · 1,544 words
Last updated

One-sentence summary

The input() function lets a program ask the user a question while it is running and store the answer typed on the keyboard in a variable.

Why does it matter?

Until now, the programs we wrote always worked with the same values. We typed the numbers into the code ourselves, and the program gave the same result every time. But real programs are not like this: a calculator learns which numbers to add, a game learns the player's name, and a robot interface learns which command to run — all from the user.

Once we learn to get input from the user, our program starts talking with us. The same code can produce a different result every time it runs, depending on the answer it receives. This is a big step that makes a program genuinely useful.

Asking a question with input()

In Python we use the input() function to ask the user for information. We write a question or a short note inside the parentheses; this text appears on the screen and guides the user.

name = input("What is your name? ")
print("Hello " + name)

When the program runs, the text What is your name? appears and the program pauses to wait. When the user types something and presses Enter, what they typed is placed into the name variable. The next line then greets them.

Here input() does two things: first it shows the message in the parentheses, then it gives the user's answer back to us. If we do not store this answer in a variable, it is lost, so we always put it in a box like name = input(...).

input() always returns text

There is a very important rule here: whatever the user types, input() always gives the answer back as text (a string). Even if the user types 5, Python does not read it as the number 5; it reads it as the text "5".

Why does this matter? Because we cannot do number operations with text. Look at this code:

number = input("Enter a number: ")
print(number + 1)   # ERROR

Even if the user types 5, this line does not run and produces an error. That is because number is actually the text "5", and Python does not know how to add the number 1 to a piece of text. Trying to add text and a number is like saying "apple + 1."

Converting text into a number

To turn the text the user typed into a number, we use the int() function. int comes from the word "integer," which means a whole number.

number = int(input("Enter a number: "))
print(number + 1)

We read this line from the inside out:

  1. input("Enter a number: ") gets a piece of text from the user (for example "5").
  2. int(...) converts this text into a number (5).
  3. number = ... puts the result into the number variable.

Now number is a real number, so we can do arithmetic with it. If the user types 5, the program prints 6.

Small note: int() is only used for whole numbers. If you need to read a decimal number (for example 3.5) from the user, you use the float() function instead. In this lesson we will work with whole numbers.

Clean output with f-strings

When we print a result, we usually want to show text and a number together. The cleanest way to do this is with an f-string. We put the letter f in front of the text and write the variables inside curly braces {}.

name = input("What is your name? ")
age = int(input("How old are you? "))
print(f"Hello {name}, you are {age} years old.")

If the user enters Ada and 12, the screen shows:

Hello Ada, you are 12 years old.

Thanks to f-strings, we do not have to join text pieces with + or convert numbers into text; Python automatically places the value inside the curly braces where it belongs.

Example: Adding two numbers

Now let us combine what we have learned. We will write a program that takes two numbers from the user and shows their sum.

number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))

total = number1 + number2

print(f"{number1} + {number2} = {total}")

When the program runs, it asks two questions in order. If the user enters 7 and 8, we see:

7 + 8 = 15

Three ideas work together here:

What would happen if we forgot to use int()? Then number1 + number2 would not be addition but joining two pieces of text side by side: 7 + 8 would give 78. This is the most common trap when working with input().

Mini practice

Write a program that asks the user's age and tells them how old they will be in 10 years.

  1. Use int(input(...)) to get the user's age and store it in an age variable.
  2. Create a new variable called future_age and give it the value age + 10.
  3. Use an f-string to print the result as a nice sentence. For example: In 10 years you will be 22 years old.

After it works, try this: remove the int() function and run the program again. Observe which error you get. This helps you remember why input() returns text.

Common mistakes

Forgetting to use int()

This is the most common mistake. Because input() always gives text, if you do not convert it with int() before doing number operations, you will either get an error or an unexpected result (text stuck together).

number = input("Number: ")   # number is text
number = int(input("Number: "))   # correct: number is really a number

Closing parentheses incorrectly

When you write int(input("...")) there are two opening parentheses, so there must also be two closing parentheses. If one is missing, Python gives a SyntaxError. Counting the code from the inside out helps.

When a letter is entered instead of a number

int() can only convert text that looks like a number. If the user types five instead of 5, the program gives an error. For now we assume the user enters the right kind of data; later we will learn how to handle these cases.

Not storing the answer in a variable

If you write input("What is your name? ") on its own, the answer is received but immediately lost. If you want to use the answer later, you must put it into a variable like name = input(...).

Safety note

When you write programs that take input from the user, there is a rule to make a habit: do not ask for or request personal or private information. Your practice programs should not ask for private things like an address, phone number, school name, password, or family details. To learn, harmless information such as a name, age, favourite colour, or a game score is enough.

Also, only run code that you wrote yourself or that you trust. Do not run a program you found online and do not understand without asking an adult first.

Lesson summary

Check questions

  1. What does the input() function do, and what type does it return the answer as?
  2. After number = input("Number: "), why does number + 1 give an error?
  3. Explain the line int(input("Age: ")) step by step, from the inside out.
  4. If the user enters 4 and 5, what would print(number1 + number2) print without using int()?
  5. Give two examples of information you should not ask the user for in a practice program.

Answers

  1. input() shows a message on the screen while the program runs and receives the answer the user types and enters with the keyboard. It always returns this answer as text (a string).
  2. Because input() gives the answer as text; number is actually a string like "4". Python cannot add a number to text, so number + 1 gives an error. You first need to convert it with int().
  3. First input("Age: ") gets a piece of text from the user. Then int(...) converts this text into a whole number. The outer assignment then stores this number in a variable.
  4. It prints 45. Without int(), both values are text; the + sign does not add two strings but joins them side by side.
  5. For example: home address, phone number, password, school name, family details. (Two of these are enough.)

Source and verification note

For “Getting Input from the User”, verification focuses on whether the relationship between Asking a question with input() and Converting text into a number remains consistent across examples. Code examples follow Python 3 syntax. Small differences may appear between environments, so examples should first be tested in a safe online editor or a local development setup.

Next lesson

Conditions: With if, elif, and else we learn how a program can make different decisions based on the user's answer.

Start QuizBack to Python Fundamentals
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.