Learning LibraryCore Coding LibraryTeens

input() in Python: ask a question, convert the answer

A break-even number is only useful for your costs. input() lets a program ask for them, which turns a one-off calculation into a tool anyone can use.

The big idea

input() displays a prompt, waits for the user to type a line, and returns that line as a string.

See it in code

1The basics

The prompt goes in the parentheses. The program pauses there until the user presses Enter, then stores what they typed:

python
launch_costs = input("Launch costs: ")
print("You entered:", launch_costs)
print(type(launch_costs))
Run it and type 12500 when it asks:
Launch costs: 12500
You entered: 12500
<class 'str'>

The digits arrived as a str. input() always returns text, whatever the user types, and it's your job to convert it.

2A step further

Wrap the call in float() to convert the text as it arrives. Now the value behaves like a number:

python
launch_costs = float(input("Launch costs: "))
profit_per_tee = 24
print("Tees to cover it:", launch_costs / profit_per_tee)
Run it. The same answer, converted:
Launch costs: 12500
Tees to cover it: 520.8333333333334

float(input(...)) evaluates inside out: ask, receive text, convert. The division then works, giving about 520.83 tees.

3At Evergreen

With two inputs, the calculator works for any budget and any price. math.ceil rounds up, because you can't sell 0.83 of a tee:

python
import math

fixed_costs = float(input("Launch costs: "))
price = float(input("Price per tee: "))
cost = 14

exact = fixed_costs / (price - cost)
tees_needed = math.ceil(exact)
print(f"Exact break-even: {exact:.2f} tees")
print(f"At ${price:.2f} a tee, sell {tees_needed} to cover ${fixed_costs:,.0f}")
Run it with a $12,500 budget and a $38 tee:
Launch costs: 12500
Price per tee: 38
Exact break-even: 520.83 tees
At $38.00 a tee, sell 521 to cover $12,500

520.83 rounds up to 521. At 520 tees Evergreen is still $20 short; the 521st tee is the one that tips it into profit.

The same idea, everywhere

Any program that adapts to its user starts this way: a quiz reads an answer, a tool reads a file name, a simulation reads how many trials to run.

Try it yourself

Run the calculator with a price of 30. Break-even jumps, because profit per tee fell from $24 to $16. Then add a third input for cost instead of fixing it at 14.

The common mistake

Doing math on the raw string. input("Launch costs: ") / 24 raises TypeError: unsupported operand type(s) for /: 'str' and 'int'. Convert at the moment you read: float(input(...)).

What it unlocks

Typed input is a string until you convert it. A user can also type something that isn't a number at all, which is what try / except is for.

Want the simpler version? Read the Kids version →