Basic

A function that asks for input

A function can read what a user types. That value comes in as text, so the function converts it before using it.

What you will see

type abc, ValueError

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Read a value inside a function.

1

Define a function that reads a number.

def ask_age(): text = input("Age? ") return int(text)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
no output yet
2

Call it and add one to the answer.

age = ask_age() print(age + 1)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
type 41, prints 42
3

Return a default when the answer is empty.

def read(prompt, default): text = input(prompt) if text == "": return default return text print(read("Name: ", "anon"))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
type blank, prints anon
4

Convert text to a number and crash on bad input.

def read_number(prompt): return int(input(prompt)) print(read_number("Num: "))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
type abc, ValueError
int cannot read letters. Real programs check or retry; this shows why the crash happens.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

input reads text, a function converts it to the kind it needs, and returns that value to the caller.
1 input reads text
↓
2 int converts it
↓
3 the function returns it
↓
4 the caller uses the value
Why this works
Input is always text

Whatever a user types arrives as text. Converting is not optional when you need a number, and it is where a crash on input usually comes from.

One question per function

A function that asks one question and returns one answer is easy to reuse. Loops and retries belong to the caller, not the question.

Examples

Read and convert

def ask_age():
    text = input("Age? ")
    return int(text)

age = ask_age()
print(age + 1)

input gives text, int turns it into a number, and the function returns the number so age + 1 works.

Keep the loop in the caller

def read_number(prompt):
    return int(input(prompt))

first = read_number("First: ")
print(first)

The function asks and returns one number. Deciding to ask again stays outside, where the reader can see it.

Guard bad input

def read_number(prompt):
    text = input(prompt)
    if text == "":
        return 0
    return int(text)

print(read_number("Num: "))

An empty answer returns 0 instead of failing. Handling the empty case is a choice the function makes.

Return the prompt too

def ask(prompt, default):
    text = input(prompt)
    if text == "":
        return default
    return text

print(ask("Name: ", "anon"))

A default covers the empty answer. The function decides the fallback, so the caller stays short.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A function that builds text
Previous Lesson
A function that builds text
Next Lesson
Where a name can be seen
Where a name can be seen →