Getting Input
Programs That Talk Back
Every program you've written so far has been a monologue. You write the code, it runs, it prints something. The user watches.
Real programs have a conversation. They ask questions, wait for answers, and do something with what they hear. A login screen asks for your password. A calculator asks for numbers. A form asks for your name.
Python gives you a single command to make this happen: input().
The input() Function
Run this. A text box will appear. Type your name and press Enter. The program greets you by name.
Here's what happened step by step:
input("What is your name? ")— Python displays the text inside the quotes as a prompt, then waits. It does nothing else until you press Enter.- Whatever you typed is returned as a value.
- That value is stored in the variable
name. print("Hello,", name)— Python uses whatever is in that box to complete the greeting.
The prompt text is optional, but always include it. A blank input() just freezes the screen with no explanation — not a great experience for whoever is using your program.
Everything Comes Back as Text
This is the single most important thing to understand about input():
It always returns a string — no matter what the user types.
If the user types 42, Python receives "42" — the text characters four and two, not the number forty-two.
Type any number and run it. The type will always be str. This surprises new programmers constantly, and it causes real errors when ignored.
Converting Types
To work with numbers from the user, you must convert the string into the type you actually need.
Python gives you conversion functions for this:
int()— converts to a whole numberfloat()— converts to a decimal numberstr()— converts to text (less often needed, but useful)
Line 2 is the key: int(age_text) takes the string "25" (or whatever was typed) and produces the integer 25. Now you can do arithmetic with it.
You can write this more compactly by wrapping input() directly inside int():
Python evaluates from the inside out: first it runs input() and gets a string, then immediately passes that string to int() and produces a number. Both versions are correct — choose whichever reads more clearly to you.
When Conversion Fails
What happens if the user types something unexpected?
Type a word instead of a number and press Enter. You'll see a ValueError — Python tried to convert "hello" into an integer and couldn't.
This is normal and expected. Handling bad input gracefully is something you'll learn to do in later lessons. For now, just know: conversion can fail, and Python will tell you clearly when it does.
Using Float for Decimals
If your user might enter a decimal number, use float() instead of int().
float() accepts both whole numbers and decimals, so it's the safer choice whenever the value could have a decimal point.
A Small Complete Program
Here's everything from the last three lessons working together — variables, types, operators, and input:
Read through it line by line. Every instruction does one clear thing. The program has a beginning, a middle, and an end. This structure — gather inputs, process them, display results — is the skeleton of most programs you'll ever write.
What You Have Learned
input() opens the door between your program and the person running it.
The key ideas:
input("prompt")displays a message, waits for the user, and returns what they typed- The return value is always a string, regardless of what was typed
- Use
int()orfloat()to convert strings into numbers before doing arithmetic - Conversion fails loudly with
ValueErrorif the input isn't a valid number - Gathering inputs → processing → displaying results is the fundamental shape of most programs
In the next lesson, you'll learn how to make decisions — how a program can do one thing or another depending on a condition.