Your tracker holds text and nothing else. This lesson adds what you spent, as numbers, and has Python add them up.
The concept
Here is your tracker as the last lesson left it. Run it:
Now run this. Look closely at the quotes:
The first line printed 4000. The second printed the characters exactly as typed. Without quotes, 1500 is a number, and + adds numbers. With quotes, it is a string, and Python keeps a string as it is.
Python can do more than add:
- subtracts, * multiplies and / divides. Look at the last two lines. 9000 / 3 came out as 3000.0, with a decimal point, even though it divides evenly.
Python has two kinds of number. Whole numbers like 1500 are integers, called int for short. Numbers with a decimal point like 3000.0 are floats, called float. Dividing with / always gives a float.
The last line shows a float's limit. A float can only hold so many digits, so the last digit of 3333.3333333333335 comes out slightly off. Your tracker keeps amounts in whole naira, so this never reaches your totals.
Numbers can be stored in variables like any other value. Arithmetic works on the names.
total holds 4000: the result of adding the values stored in bus_fare and lunch.
The syntax
bus_fare = 1500
total = bus_fare + 2500A number is written as digits, with no quotes and no commas. Python allows underscores to make long numbers easier to read: 1_500_000 is the same number as 1500000.
+adds:1500 + 2500is4000.-subtracts:5000 - 1200is3800.*multiplies:1500 * 3is4500./divides:9000 / 3is3000.0, always a float.
When one line mixes operators, Python multiplies and divides first, then adds and subtracts. Parentheses go before everything. Run this to see both orders:
A minimal working example
bus_fare = 1500
lunch = 2500
data_bundle = 3500
total = bus_fare + lunch + data_bundle
print(total)Three amounts, one total, one line of output: 7500. Each name holds a whole number, so the total is a whole number too.
Common mistakes
You write an amount with a comma. People write 1,500 on paper, so this one is very common. Run it:
The console shows (1, 500), not 1500. In Python a comma separates values, so 1,500 means two numbers, 1 and 500, grouped together. Python calls that group a tuple. You will not need tuples in this track. If you add that group to a number, you get TypeError: can only concatenate tuple (not "int") to tuple. Write 1500, or 1_500 if you want a separator.
You put quotes around a number. print("1500" + "2500") prints 15002500. Both values are strings, and + on two strings joins them end to end. Remove the quotes to get 4000. Sometimes the quotes are not yours to remove, because the amount arrived as text. A later lesson in this part handles that case.
You expect a total to update itself. Run this:
It prints 4000, not 4500. The total line added 1500 and the value lunch held at that moment. Changing lunch afterwards does not redo the sum. If a total depends on other values, assign it after they are final.
Try it yourself
Your tracker gets its first numbers. Use the amounts given, or your own in whole naira. Write digits only, no commas.
- Run the starting code below. Confirm the console shows your title, the month, a blank line, then
Nothing spent yet. - Under the
monthline, add a blank line, then three expenses on three lines:bus_fare = 1500,lunch = 2500anddata_bundle = 3500. Run it. Confirm the output has not changed. Storing a value prints nothing. - Under your three expenses, add
total = bus_fare + lunch + data_bundle. Change the last line toprint(total). Run it. Confirm the last line is your three amounts added together:7500with the amounts given. - Delete the
status = "Nothing spent yet"line. Nothing uses it now. Run it. Confirm the output has not changed. - Break it on purpose: put a comma in your
bus_fareamount, for examplebus_fare = 1,500. Run it. Confirm the console shows aTypeErrorthat mentions a tuple and points at yourtotalline. Nothing printed this time, because thetotalline runs before everyprint. - Fix it: remove the comma. Run it. Confirm your title, month and total return.
Your tracker adds up what you spent. The last line is a bare number, though, and a reader cannot tell what it means. The next lesson puts that number inside a sentence.