Skip to main content

Programming fundamentals · Beginner · 14 min

Record what you spend from the keyboard with input

By Padding · Updated 18 Nov 2026
Python 3.15.2 · python:3.15-slim (Debian)

You will change the budget tracker so it asks for your budget and an expense when it runs, turns the typed text into numbers and prints the right balance.

On this page

In part 1 your budget tracker had its numbers written into the code. To track a different month you had to open the file and edit it. A real tracker asks you instead: you run it, it asks what your budget is, you type a number, and it does the sums.

In this part you use Python's input() to read what you type at the keyboard. Along the way you meet the most common surprise for new programmers: text that looks like a number is not a number, so "500" + "200" gives "500200". You fix it by converting the text, and you end with a tracker that asks two questions and prints the correct balance.

The examples use python3, as in part 1. On Windows outside WSL, type py instead.

Step 1 · Open the tracker folder

All the files in this series live in one folder. Go to it (the command creates it if you are starting here):

Bash
mkdir -p ~/budget-tracker
cd ~/budget-tracker
pwd

mkdir -p makes the folder only if it is missing, so it does nothing to your part 1 work. cd moves into the folder, and pwd prints where you are.

Check it worked

The last line ends in /budget-tracker. If you did part 1, ls shows budget.py and PLAN.txt. You replace that file later in this part, so keep a copy if you want to compare: cp budget.py budget_part1.py.

Step 2 · Ask a question with input()

Before touching the tracker, try input() on its own. Create ask.py. The command below writes the file for you; everything between the two PY lines is the Python code, and you can paste those lines into your editor and save them as ask.py instead:

Bash
cat > ask.py <<'PY'
currency = input("Which currency do you use? ")
print(f"Amounts will be in {currency}.")
PY

Line by line:

  • input("Which currency do you use? ") prints the text in brackets, called the prompt, then stops and waits. Whatever you type, up to the moment you press Enter, comes back as the result. The space at the end of the prompt keeps your answer from touching the question mark.
  • currency = ... stores that result in a variable, exactly as you stored numbers in part 1.
  • The print line uses an f-string, as in part 1: the {currency} part is replaced by the variable's value.

Run it and answer the question:

Bash
python3 ask.py

Check it worked

The program shows Which currency do you use? ` and waits with the cursor after it. Type `naira (or your own currency) and press Enter. The next line is Amounts will be in naira. The program only continues once you press Enter.

Step 3 · Add two typed amounts and see the bug

Now ask for two expenses and add them. Write bug.py:

Bash
cat > bug.py <<'PY'
first = input("First expense: ")
second = input("Second expense: ")
total = first + second
print(f"Total spent: {total}")
print(type(first))
PY

Run it, typing 500 and then 200:

Bash
python3 bug.py

The total is 500200, not 700. The last line explains why: type(first) reports what kind of value first holds, and it says <class 'str'>. A str (short for string) is text. input() always gives back text, because the keyboard sends characters, and Python does not guess that you meant a number. For text, + joins the two pieces end to end, so "500" + "200" is "500200".

Subtraction is worse: Python has no rule for taking one piece of text away from another, so "50000" - "12500" stops the program with TypeError: unsupported operand type(s) for -: 'str' and 'str'.

Check it worked

Your output shows Total spent: 500200 and <class 'str'>. If you see 700, you have already wrapped input() in int() on the first two lines; remove it for now so you can see the bug, because the next step adds it back on purpose.

Step 4 · Turn the text into a whole number with int()

int() takes text made of digits and gives back a whole number, called an integer. Change bug.py so each answer is converted as soon as it is read:

Bash
cat > bug.py <<'PY'
first = int(input("First expense: "))
second = int(input("Second expense: "))
total = first + second
print(f"Total spent: {total}")
print(type(first))
PY

int(input("First expense: ")) runs from the inside out. input() runs first and returns the text "500". That text goes straight into int(), which returns the number 500, and the number is what first stores. Run it again with the same answers:

Bash
python3 bug.py

int() ignores spaces before and after the digits, so ` 500 ` with stray spaces still works.

Check it worked

The output is now Total spent: 700, and the type line says <class 'int'>. Same keys typed, different result, because the program now treats them as a number.

Step 5 · Decide between int() and float()

Not every amount is whole. float() converts text with a decimal point, such as 12.50, into a floating-point number. int() refuses that same text. Try both, and one sum that shows why money and decimals need care:

Bash
cat > decimals.py <<'PY'
print(float("12.50"))
print(0.1 + 0.2)
print(10 + 20)
PY
python3 decimals.py
  • float("12.50") prints 12.5. The trailing zero is dropped: to Python it is the same number.
  • 0.1 + 0.2 prints 0.30000000000000004. Computers store floats in binary, and most decimal fractions, like 0.1, cannot be stored exactly, so tiny errors appear. They are small, but they show up in printed totals and in comparisons such as "is the balance exactly zero?".
  • 10 + 20 prints 30. Whole numbers are exact.

For this tracker, use whole units of your currency and int(): 50000 naira, 2500 shillings, 40 dollars. If you need the smaller unit, count in it instead, for example 1250 cents rather than 12.50 dollars. Every sum then stays exact. Part 7 of this series saves your amounts to a file, and whole numbers keep that simple too.

Check it worked

You see three lines: 12.5, 0.30000000000000004 and 30. The middle line is the reason the tracker uses int().

Step 6 · Make the tracker ask for its numbers

Now rewrite budget.py so it asks for the budget and one expense, instead of having them written in. This replaces the part 1 version:

Bash
cat > budget.py <<'PY'
budget = int(input("Monthly budget: "))
spent = int(input("Amount spent: "))
balance = budget - spent

print(f"Budget {budget}, spent {spent}, left {balance}")
PY

What each line does:

  • The first two lines ask a question each, convert the answer with int() and store it, as in bug.py. Type whole units only, as the previous step explained.
  • balance = budget - spent works now, because both values are numbers. In part 1 this variable was called left; from here on the series calls it balance, the usual word for what remains in an account. The printed line still says left.
  • The print line builds the same summary you printed in part 1.

Part 3 starts from exactly these five lines, so keep them as they are.

Run it and type 50000, then 12500:

Bash
python3 budget.py

You can also hand a program its answers without typing them each time. printf prints text, \n stands for pressing Enter, and | sends that text to the program as if you had typed it. This is useful for testing the same numbers again and again:

Bash
printf '50000\n12500\n' | python3 budget.py

The prompts and the summary then run together on one line, because nobody pressed Enter on screen. The sums are the same.

Check it worked

After the two questions, the program prints Budget 50000, spent 12500, left 37500. Run it again with different numbers, for example 20000 and 25000, and you get left -5000: you overspent. Part 3 makes the tracker warn you about that.

Step 7 · See what happens when you type letters

Programs meet people, and people type unexpected things. Run the tracker and type fifty thousand at the first question:

Bash
python3 budget.py

The program stops with a traceback, Python's report of where things went wrong. Read it from the bottom up. The last line is the important one:

Text
ValueError: invalid literal for int() with base 10: 'fifty thousand'

ValueError means a function got a value of the right kind (text) that it cannot use. int() understands whole numbers written with digits, such as 50000 or -200. It also accepts the underscore form from part 1, so 50_000 works too. The lines above name the file and line number where it happened.

The same error appears for amounts written the way many people write them by hand or on a phone keyboard: 50,000 with a comma, ₦50000 or $40 with a currency sign, or 12.50 with a decimal point. For now, type digits only. Part 8 of this series catches this error and asks the question again instead of stopping.

Check it worked

You see a traceback ending in the ValueError line above, and no summary is printed. Run python3 budget.py once more with 50000 and 12500 to confirm it still works with digits.

Remove the practice files, and keep budget.py, because part 3 builds on it:

Bash
rm -f ask.py bug.py decimals.py
ls

ls lists budget.py and PLAN.txt from part 1 (if you did it), plus budget_part1.py if you made the copy in the first step.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.