Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.10 — Reading Input with input()
Python · Example 2 of 2

Text in, numbers out

typed_a and typed_b are what a person would type. Notice that + joins text but adds numbers.

input(prompt) pauses the program, shows the prompt, waits for the user to type a line and press Enter, and returns what they typed as a string. Convert it with int() or float() before calculating, and check it before you trust it. input() does not work in the browser panels on this site, so the examples here use fixed values.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a program that asks for a name and prints Hello, <name>!. Then write the same program with a fixed value in place of input() so it can run here.

02

Given the text typed = " 42 ", print the number plus 8.

03

Given the texts a = "3.5" and b = "1.5", print their sum as a number.

Explain it without notes

01

What type does input() return? Why does input("Age: ") + 1 fail?

02

How do you read a whole number from the user, and what can go wrong?

03

Why do the examples in this course use fixed values instead of input() in the Run button?

04

What is sys.argv?

Text in, numbers out
Sign in to run this example in your browser.

Expected output

75
12
5.0
KM