So far our are step by step and they always follow the same path from start to end. But sometimes a decision has to be made and different steps will be taken depending on the decision made.
In Python we can use if, if/else and if/elif/else statements to evaluate data and create paths through a program.
Copy the starter code into your IDE and complete the worksheet.
money = int(input("How much money do you have?"))
if money > 20:
print("Get a haircut.")
print("Buy a hat.")
print("Go to the cinema.")
else:
print("Watch tv.")
print("End of program.")
*note: End of program is not associated with the if/else logic. It will always be displayed!
Relational operators are used to compare data when making decisions using if/else
Here they are:
| Relational operator | Description |
|---|---|
| > | greater than |
| < | less than |
| >= | greater than or lequal to |
| <= | less than or equal to |
| == | the same as |
| != | not the same as |
integratedsyntax development environment machine code translate type errorcompile