Getting Input from the User¶
By the end of this lesson you'll be able to:
- Use
input()to pause a program and ask the user a question - Convert text input to an integer with
int()so you can use it as a number - Draw a shape whose size or count changes based on what the user types
Every program you've written so far runs the same way every time. input() changes that — it pauses the program, displays a prompt, and waits for the user to type something before continuing. Whatever the user types becomes a value your program can use.
Welcome to This Lesson!
What if you could tell Monty how many shapes to draw — while the program is running?
That's what user input does! Let's code it together!
The input() Function¶
input("prompt text") displays a message in the output box and waits. When the user types something and presses Enter, input() returns what they typed — always as a string (text).
If you need a number, wrap input() with int() to convert the string:
1 2 | |
Or combine them on one line:
1 | |
Scratch Bridge
In Scratch you used the "Ask ___ and wait" block, which stored the answer in the answer variable.
In Python, input("question") does the same thing — it asks, waits, and returns the answer!
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
What Do You Think Will Happen?
When you click Run, a prompt will appear asking how many circles to draw.
If you type 5, how many circles will appear? What if you type 0?
Make your guess — then click Run and find out!
Try It Now¶
Click Run, type a number in the output box when prompted, then press Enter. No account needed — everything runs in your browser.
Were you right? The program drew exactly as many circles as you typed — the loop runs count times, using i % len(colors) to cycle through the color list without going out of range.
How It Works¶
input() always returns a string — even if the user types 5, Python stores "5" (text), not 5 (number). Calling int() on it converts "5" to the integer 5. Only after that can you use it in range(count).
i % len(colors) is the remainder when i is divided by the number of colors. This cycles the index back to 0 when i reaches the end of the list — so you can draw more circles than there are colors without an IndexError.
Explanation Table¶
| Line | What it does |
|---|---|
input("prompt") |
Displays the prompt and waits for the user to type and press Enter |
int(...) |
Converts the returned string to an integer |
count = int(input(...)) |
Combines both steps in one line |
i % len(colors) |
Cycles through color indexes without going out of bounds |
Always Convert to int
If you forget int() and try to use the input directly in range(count), you'll get a
TypeError: 'str' object cannot be interpreted as an integer.
input() always returns a string — you must convert it if you need a number.
Learning Check¶
Your Turn — Ask for the Radius Too
The program below asks for the number of circles but always draws them with radius 20.
Add one line to also ask the user for the radius, store it in radius, and use it
in monty.circle(radius) instead of monty.circle(20).
Experiments¶
Try these changes. Remember to type a value in the prompt when you click Run!
-
Ask the user for the radius instead of counting circles — use
radius = int(input("Circle radius? "))and draw one circle with that radius. You'll know it worked when typing different numbers produces circles of different sizes. -
Ask for a color name:
my_color = input("Pen color? ")and usemonty.color(my_color)before the loop. You'll know it worked when typing'blue'gives blue circles and'green'gives green ones. -
Try typing a decimal like
3.5at the prompt withint(). Notice theValueError. Now changeint()tofloat()so decimals are accepted. You'll know it worked when typing3.5no longer crashes the program. -
Add a
print()at the end that says how many circles were drawn:print("Drew", count, "circles!"). You'll know it worked when that message appears in the output box after the drawing finishes. -
Ask for the number of sides per shape. Use the answer to draw polygons instead of circles. You'll know it worked when typing
3draws triangles,4draws squares, etc.
Great Work!
Your programs can now talk to the user and change their behavior on the fly —
that's what makes software feel alive! Let's code it together!