Nishaglobal Education logo

Getting input from the user

Ask the user questions with input(), turn their answers into numbers with int() and float(), and build a small interactive age and fees calculator.

Lesson 5 of 1420 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Ask the user a question with input() and store the answer
  • Convert typed answers into numbers with int() and float()
  • Recognise the ValueError that bad input causes
Outline
  1. What you'll learn and why it matters
  2. Asking a question with input()
  3. Turning answers into numbers
  4. Worked example: course fees calculator
  5. Practice
  6. Common mistakes
  7. Recap
  8. Quiz
  9. Mark lesson complete

What you'll learn and why it matters

So far your programs have always done the same thing. Real programs respond to people: they ask for a name, an amount or a choice. In this lesson your programs start a conversation with the user.

Asking a question with input()

input() shows a message, waits for the user to type something and press Enter, and gives you what they typed as a string:

Python
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")

Output (you type: Fatima)

What is your name? Fatima
Nice to meet you, Fatima!

When you run this in IDLE, the program pauses after the question. Click in the shell, type a name and press Enter. Here is what the screen looks like when you type Fatima:

Screen showsWho wrote it
What is your name? Your program (the prompt)
FatimaYou typed this, then pressed Enter
Nice to meet you, Fatima!Your program

Turning answers into numbers

input() always gives you a string, even if the user types digits. "25" with quotes is text, not a number, so you cannot do maths with it yet. Convert it first:

FunctionConverts toExampleResult
int()whole numberint("25")25
float()decimal numberfloat("2.5")2.5
str()stringstr(25)"25"
Python
birth_year = int(input("What year were you born? "))
age = 2026 - birth_year
print(f"You turn {age} in 2026.")

Output (you type: 2005)

What year were you born? 2005
You turn 21 in 2026.

Read the first line from the inside out: input() asks the question and returns the text "2005", then int() turns it into the number 2005, which is stored in birth_year.

Worked example: course fees calculator

Python
course = input("Course name: ")
fee = float(input("Monthly fee: "))
months = int(input("Number of months: "))

total = fee * months
print(f"{course.title()} costs {total:.2f} for {months} months.")

Output (you type: spoken english, 1499, 3)

Course name: spoken english
Monthly fee: 1499
Number of months: 3
Spoken English costs 4497.00 for 3 months.

With the answers spoken english, 1499 and 3, the program multiplies 1499.0 by 3 to get 4497.0 and formats it with two decimal places. The fee uses float() because fees can have paise or cents; the months use int() because months are whole.

Practice

Guided exercise

Rectangle area

Ask for the width and height of a room in metres (they may have decimals) and print the area.

  1. Use float(input(...)) for each measurement.
  2. Multiply them to get the area.
  3. Print the result with an f-string and :.1f for one decimal place.
Show the solution
Solution
width = float(input("Width in metres: "))
height = float(input("Height in metres: "))
area = width * height
print(f"Area: {area:.1f} square metres")

Output (you type: 3.5, 4)

Width in metres: 3.5
Height in metres: 4
Area: 14.0 square metres

How to get there: Measurements can have decimals, so float() is the right conversion. With 3.5 and 4 the area is 14.0.

Your turn

Minutes until a deadline

Ask for a number of hours (a whole number) and print how many minutes that is, as 5 hours is 300 minutes.

Show the solution
Solution
hours = int(input("Hours: "))
print(f"{hours} hours is {hours * 60} minutes.")

Output (you type: 5)

Hours: 5
5 hours is 300 minutes.

How to get there: Convert the answer with int() before multiplying. Without it, "5" * 60 would repeat the text "5" sixty times instead of doing maths.

Common mistakes

The user types a word instead of a number
age = int(input("Age: "))

Python stops with this error (you type: twenty)

ValueError: invalid literal for int() with base 10: 'twenty'

Doing maths on the answer without converting

You see: TypeError: can only concatenate str (not "int") to str, or "5" * 3 gives 555

Fix: Wrap input() in int() or float() when you need a number.

The user types text or a decimal into int()

You see: ValueError: invalid literal for int() with base 10

Fix: Use float() if decimals are allowed. In lesson 13 you will learn to catch this error and ask again.

Forgetting the brackets on input

You see: The program does not wait, or prints something like <built-in function input>

Fix: Write input("Question ") with brackets.

Recap

  • input("Question ") shows a prompt and returns what the user types, always as a string.
  • Convert answers with int() for whole numbers and float() for decimals before doing maths.
  • Read nested calls from the inside out: int(input("Age: ")) asks first, then converts.
  • Typing a word where a number is expected causes a ValueError.

Key terms

Prompt
The message shown to the user when the program asks for input.
Type conversion
Turning a value of one type into another, such as int("5").
ValueError
An error that happens when a value cannot be converted, such as int("hello").

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. The user types 7 when `x = input("Number: ")` runs. What is `x`?

  2. 2. What does `"4" * 3` give?

  3. 3. Which line reads a price that might have decimals?

  4. 4. `int(input("Age: "))` stops with ValueError. What did the user most likely type?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.