Course outline · 0% complete

0/32 lessons0%

Course overview →

Reading Input

lesson 3-2 · ~11 min · 8/32

input() reads one line of text

Without input, every run of a program does exactly the same thing. input() is the first way your programs react to a user, and the read-a-line-then-convert shape you learn here returns unchanged when you read files and network data later.

input() pauses the program, waits for the user to type a line, and hands that line back as a string, always. Even if the user types 17, you get the string "17".

name = input()          # user types: Ada
age = int(input())      # user types: 17, converted to the int 17

That int(input()) combo is the standard way to read a number. Forgetting the conversion is the classic beginner bug: "17" + 3 crashes, and "17" * 3 gives "171717" (Python repeats strings when you multiply them).

In these exercises the Run panel supplies the typed lines for you, listed as stdin (standard input, the program's incoming text stream).

keyboard17input()"17" strint(...)17 intinput() always yields a string. Convert it yourself.
The typed line arrives as the string "17". int() turns it into a number you can do math with.

Reading a name and a number

The stdin panel already holds the two lines this program will read, Ada and 17, so the calls to input consume them in order.

name = input()
age = int(input())
print(f"Hello {name}, in 3 years you will be {age + 3}")

Input

Ada
17

Output

Hello Ada, in 3 years you will be 20

The first input() keeps its value as a string, which is right for a name. The second wraps the call in int so that age holds a number, and that conversion is what allows age + 3 to compute 20 inside the f-string. Without it the same expression would fail, since adding a number to text is not something Python will do on its own.

After x = input() with the user typing 5, the value of x * 2 is "55".

The call returned the string "5", and multiplying a string by an integer repeats it, so "5" * 2 produces "55". No arithmetic took place at all.

Getting 10 requires converting on the way in, with x = int(input()). This is the single most common beginner bug in interactive programs, and it is dangerous precisely because it does not crash. The program keeps running and quietly produces a wrong answer.

Reading two numbers and reporting statistics about them is the classic shape of an interactive script. Both lines are converted on the way in, and the f-strings do the formatting on the way out.

a = int(input())
b = int(input())
print(f"Sum: {a + b}")
print(f"Average: {(a + b) / 2:.1f}")

Input

7
10

Output

Sum: 17
Average: 8.5

Each int(input()) reads one line and converts it, so a and b hold real numbers. The parentheses in (a + b) / 2 matter, because without them only b would be divided. Since / always produces a float, the average arrives as 8.5 rather than being cut down to a whole number, and the :.1f spec keeps it displayed to a single decimal place.

Combining input with the // and % pair from lesson 1-3 turns a raw count of minutes into a readable duration.

total = int(input())
print(f"{total // 60} h {total % 60} min")

Input

150

Output

2 h 30 min

The first line reads and converts in a single step, a compact form worth getting used to. Both divisions then happen inside the braces of one f-string: total // 60 counts the two whole hours in 150 minutes, and total % 60 reports the 30 minutes left over. The letters h and min sit outside the braces as literal text, so the whole line of output comes from a single print.