Skip to content

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!

Monty waving welcome 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
answer = input("How many circles? ")
count = int(answer)

Or combine them on one line:

1
count = int(input("How many circles? "))

Scratch Bridge

Monty with a tip 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
import turtle
monty = turtle.Turtle()
monty.shape('turtle')
monty.speed(10)

count = int(input("How many circles to draw? (try 3 to 10) "))
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink']

monty.penup()
monty.goto(-150, 0)

for i in range(count):
    monty.color(colors[i % len(colors)])
    monty.pendown()
    monty.begin_fill()
    monty.circle(20)
    monty.end_fill()
    monty.penup()
    monty.forward(50)

monty.hideturtle()

What Do You Think Will Happen?

Monty thinking 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

Monty warning 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

Monty thinking 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!

  1. 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.

  2. Ask for a color name: my_color = input("Pen color? ") and use monty.color(my_color) before the loop. You'll know it worked when typing 'blue' gives blue circles and 'green' gives green ones.

  3. Try typing a decimal like 3.5 at the prompt with int(). Notice the ValueError. Now change int() to float() so decimals are accepted. You'll know it worked when typing 3.5 no longer crashes the program.

  4. 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.

  5. Ask for the number of sides per shape. Use the answer to draw polygons instead of circles. You'll know it worked when typing 3 draws triangles, 4 draws squares, etc.

Great Work!

Monty celebrating 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!