Your tracker reports what you spent. It cannot tell you whether that is too much. This lesson adds a budget and asks Python the first question your program can answer: is the total over budget?
The concept
Here is your tracker at the end of Part 1. Run it:
Every line it prints is a value it worked out. Now run this:
Python did not print a number. It printed False, then True. The first line asked "is total greater than budget?" and the answer was no. The second asked "is total less than budget?" and the answer was yes.
Writing two values with > or < between them is a comparison. A comparison is a question, and Python answers it with True or False. Here are the others:
== asks "are these equal?". != asks "are these different?". >= and <= include the equal case: 12500 >= 12500 is True.
An answer is a value like any other, so you can store it:
True and False have a type of their own, called bool. Every comparison gives back a bool.
Text can be compared too. Run this:
Two strings are equal only when every character matches. A capital letter or a space at the end is enough to make them different.
The syntax
over_budget = total > budgetRead it right to left. Python works out total > budget first, which gives True or False. Then = stores that answer under the name over_budget.
There are six comparison operators.
==equal to.!=not equal to.<less than.>greater than.<=less than or equal to.>=greater than or equal to.
In <= and >=, the equals sign always comes second. True and False start with a capital letter.
A minimal working example
total = 12500
budget = 15000
over_budget = total > budget
print(f"Budget: ₦{budget:,}")
print(f"Over budget: {over_budget}")Output: Budget: ₦15,000, then Over budget: False. An f-string prints a bool the same way it prints any other value.
Common mistakes
You use one equals sign to compare. This trips up almost everyone. Run this:
TypeError: 'total' is an invalid keyword argument for print()One = stores a value. Python did not read your line as a question at all. It thought you were setting an option called total on print. To ask whether two values are equal, write ==.
You compare a number with text. Run this:
TypeError: '>' not supported between instances of 'int' and 'str'"15000" has quotes, so it is a string. Python will not say whether a number is bigger than some text. Convert the text with int() first, the way you did with your fuel receipt.
You write the operator the wrong way round. total =< budget gives SyntaxError: invalid syntax. The equals sign goes second: <=.
You write true in lowercase. Run this:
Lowercase true is not a value Python knows, so it treats it as a variable name. Nothing was assigned to that name. Read the end of the error: Python suggests 'True'.
Try it yourself
You will give your tracker a budget, then ask whether you went over it.
- Run the starting code below. Write down your total.
- Under the
monthline, addbudget = 15000, or your own monthly budget in whole naira. At the end of the file, addprint(f"Budget: ₦{budget:,}"). Run it. Confirm the last line shows your budget. - Under the
smallestline, addover_budget = total > budget. At the end of the file, addprint(f"Over budget: {over_budget}"). Run it. Confirm the last line saysFalseif your total is below your budget, andTrueif it is above. With the amounts given, it isFalse. - Change your budget to
10000and Run it. Confirm the answer flips. Then put your budget back. - Break it on purpose: put quotes around your budget, for example
budget = "15000". Run it. Confirm the console shows aTypeErrorthat mentions'>','int'and'str', pointing at yourover_budgetline. - Fix it: remove the quotes. Run it. Confirm your report returns with its
Over budgetline.
Your tracker can now answer a question about your spending. It still prints the answer the same way every time, though: a bare True or False. The next lesson uses that answer to decide what to print.