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):
mkdir -p ~/budget-tracker
cd ~/budget-tracker
pwdmkdir -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:
cat > ask.py <<'PY'
currency = input("Which currency do you use? ")
print(f"Amounts will be in {currency}.")
PYLine 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
printline uses an f-string, as in part 1: the{currency}part is replaced by the variable's value.
Run it and answer the question:
python3 ask.pyCheck 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:
cat > bug.py <<'PY'
first = input("First expense: ")
second = input("Second expense: ")
total = first + second
print(f"Total spent: {total}")
print(type(first))
PYRun it, typing 500 and then 200:
python3 bug.pyThe 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:
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))
PYint(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:
python3 bug.pyint() 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:
cat > decimals.py <<'PY'
print(float("12.50"))
print(0.1 + 0.2)
print(10 + 20)
PY
python3 decimals.pyfloat("12.50")prints12.5. The trailing zero is dropped: to Python it is the same number.0.1 + 0.2prints0.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 + 20prints30. 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:
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}")
PYWhat each line does:
- The first two lines ask a question each, convert the answer with
int()and store it, as inbug.py. Type whole units only, as the previous step explained. balance = budget - spentworks now, because both values are numbers. In part 1 this variable was calledleft; from here on the series calls itbalance, the usual word for what remains in an account. The printed line still saysleft.- The
printline 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:
python3 budget.pyYou 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:
printf '50000\n12500\n' | python3 budget.pyThe 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:
python3 budget.pyThe 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:
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:
rm -f ask.py bug.py decimals.py
lsls 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
- Make the tracker react to the numbers it reads, warning when you overspend: warn when spending goes over budget with if and else.
- Go back to the starting point of this program: write your first program, a budget tracker that prints a balance.
- Learn more about
str,int,floatand the other kinds of value Python has: understand values and types in Python.
Part of these paths
Related tutorials
- Write your first program: a budget tracker that prints a balanceYou will write budget.py, a short Python 3.15 program that stores a monthly budget and two expenses in variables, works out what is left and prints it in your own currency, plus a plain-words plan for the rest of the tracker.
- Warn when spending goes over budget with if and elseYou will teach your Python budget tracker to make decisions, so it warns you when you overspend, cautions you when less than a tenth of the budget is left, and otherwise shows the balance.
- Understand values and types in Python: int, float, str, bool and NoneYou will explore Python's five basic types in the REPL and in small scripts, convert between them, see why 0.1 + 0.2 is not 0.3, and end with a self-checking cheat sheet built from what you ran.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials