Your tracker adds amounts you typed as numbers. This lesson adds one that arrives as text, the way a total copied from a receipt would. Your program converts it before adding it.
The concept
Here is your tracker as the last lesson left it. Run it:
Now run this. The digits are the same on both lines:
The first line printed 2500300. The second printed 2800. With quotes, the values are strings, and + joins strings end to end. Without quotes, they are numbers, and + adds them.
So Python is always tracking what kind of value it holds. That kind is called the value's type. You can ask Python for it:
int is a whole number. float is a number with a decimal point. str is Python's short name for a string. type() is a built-in function that reports which one a value is.
What happens when the two kinds meet? Run this:
TypeError: can only concatenate str (not "int") to strConcatenate means join text end to end. Python sees a string on the left, so it expects to join another string onto it. It finds an int instead and stops. Python will not guess whether you meant "add" or "join". You have to say.
Why would an amount be text at all? Values that come from outside your program usually arrive as text: something a person typed, a line in a file, a field on a website. In this lesson, quotes stand in for that. The fix is the same wherever the text came from. Convert it:
int(receipt_total) read the text "4200" and gave back the number 4200. The program stored that number in fuel, and fuel + 300 added up to 4500. The last line confirms it: fuel is an int.
The syntax
fuel = int("4200")
price = float("2500.50")
label = str(4200)int(...)converts to a whole number.float(...)converts to a number with a decimal point.str(...)converts to text.
Each one gives back a new value. It does not change the value you passed in, so store the result in a variable.
int accepts text made of digits, with or without spaces around them: int(" 4200 ") is 4200. It refuses anything else, including commas and decimal points.
A minimal working example
receipt_total = "4200"
fuel = int(receipt_total)
total = 1500 + fuel
print(f"Total spent: ₦{total:,}")The amount arrives as text, gets converted once, and from then on it is an ordinary number. Output: Total spent: ₦5,700.
Common mistakes
You add text and a number. Depending on which side the text is on, the message changes. "2500" + 300 gives TypeError: can only concatenate str (not "int") to str. 300 + "2500" gives TypeError: unsupported operand type(s) for +: 'int' and 'str'. The wording differs. The cause is the same: a str and an int on either side of +. Find the value that is still text and convert it.
You convert but never store the result. This trips up almost everyone. Run this:
The TypeError points at line 3, even though line 2 converted fuel. int(fuel) made a number, and then nothing kept it. fuel still holds the text "4200". Write fuel = int(fuel) so the variable holds the number.
The text has a comma in it. Run this:
ValueError: invalid literal for int() with base 10: '4,200'"Invalid literal" means the text is not written the way a whole number is written in code. "Base 10" is ordinary counting, and you can ignore it. Text like "2500.50" fails the same way: use float for that. Text with a comma cannot be converted as it is. Part 3 of this track shows how your program can skip a line like this instead of stopping.
Try it yourself
A fourth expense arrives as text: a fuel receipt. You will see it fail, then convert it.
- Run the starting code below. Write down your total.
- Under the
data_bundleline, addfuel_receipt = "4200", with the quotes. Then addprint(type(fuel_receipt))as the very last line of the file. Run it. Confirm the last line of output is<class 'str'>. - Break it on purpose: add
+ fuel_receiptto the end of yourtotalline. Run it. Confirm the console showsTypeError: unsupported operand type(s) for +: 'int' and 'str', pointing at yourtotalline. Nothing printed, because thetotalline runs before everyprint. - Fix it: under
fuel_receipt, addfuel = int(fuel_receipt). In yourtotalline, changefuel_receipttofuel. Change the last line toprint(type(fuel)). Run it. Confirm your total is 4,200 more than the one you wrote down, and the last line is<class 'int'>. - Delete the
print(type(fuel))line. It was only there for checking. Run it. Confirm your report ends with the total again.
Your tracker can take an amount that arrived as text. It also has four separate expense names now. Every new expense means another name and a longer total line. The next lesson keeps all your expenses under one name.