Skip to content
IT Vizag
← Python Basics

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

Getting input from the user

Lesson 7 of 10 · 1 min read

Ask questions and convert answers into numbers.

🎬 Video version coming soon — read it now

input() pauses the program, waits for the user to type something, and gives you what they typed.

🔒 Code example: sign up free (above) to unlock it.

In Colab a text box appears under the cell. Type, then press Enter.

input() always gives you text

This is the number-one beginner trap:

🔒 Code example: sign up free (above) to unlock it.

age holds the text "20", not the number 20. Convert it:

🔒 Code example: sign up free (above) to unlock it.
  • int() for whole numbers
  • float() for decimals, such as prices or weights
  • str() to turn a number back into text

A small interactive program

🔒 Code example: sign up free (above) to unlock it.

What if the user types rubbish?

If someone types twenty when you ask for a number, int() crashes with a ValueError. That's normal for now. In the Functions & Clean Code course you'll learn try/except to handle it politely.

Tip: Always end your question with a space, "Your age: ", so the user's typing doesn't stick to your text.

In one line

input() reads what the user types as text, so wrap it in int() or float() when you need a number.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend