🐍 Python: Casting, Flowcharts, BIDMAS & Errors
Data Types, Planning, Order of Operations & Debugging
What is Casting?
Casting is the process of ensuring data is set to the correct data type. In Python, when the user inputs data and it is stored in a variable, you need to inform the program what type of data it is.
input() function captures user input as a string data type. If you want to perform calculations, you must cast it to int() or float().
Without Casting (Wrong Result)
num1 = input("Enter a number: ")
answer = num1 * 2
print("The answer is", answer)
Why? Python treats num1 as a string, so "5" * 2 becomes "55" (string repetition), not 10.
With Casting (Correct Result)
| Code | What it does |
|---|---|
num1 = int(input("Enter a number: ")) |
Casts input to an integer (whole number) |
num1 = float(input("Enter a number: ")) |
Casts input to a float (decimal number) |
num1 = int(input("Enter a number: "))
answer = num1 * 2
print("The answer is", answer)
Essential Vocabulary
- Casting: The process of ensuring data is set to the correct data type
Fix MealCost.py
Original Code (with errors):
money = input("How much money do you have? ")
meal = input("How much did the meal cost? ")
left = money - meal
print("After your meal you have,", left, "left.")
money = float(input("How much money do you have? "))
meal = float(input("How much did the meal cost? "))
left = money - meal
print("After your meal you have,", left, "left.")
Rabbit Age Calculator
A rabbit's age is approximately 9 times the age of a human. Create a program that calculates rabbit age.
age = int(input("Enter the age of the rabbit: "))
rabbitAge = age * 9
print("The age of the rabbit in rabbit years is", rabbitAge)
Square Area Calculator
Ask user for the length of one side of a square and calculate the area.
length = int(input("Enter the length of one side of a square: "))
area = length * length
print("The area of the square is:", area)
Car Speed Calculator
Calculate speed using distance and time (Speed = Distance ÷ Time).
distance = int(input("Enter the distance in meters: "))
time = int(input("Enter the time in seconds: "))
speed = distance / time
print("The car was travelling at a speed of", speed, "meters per second")
Note: Use float() instead of int() if you want decimal precision for the inputs.
Planning with Flowcharts
To code a calculation program, you first need to plan. This allows you to break down the problem and work through a solution. A common method is creating a flowchart.
Key Flowchart Symbols
| Symbol | Shape | Purpose |
|---|---|---|
| Start/Stop | Oval | Used at the start and end of the flowchart |
| Process | Rectangle | Used when something needs to happen (e.g., calculations) |
| Input/Output | Parallelogram | Used when data needs to be input or output |
Flowchart → Python Code
Flowchart Steps:
- Start
- INPUT number1
- INPUT number2
- total = number1 + number2
- OUTPUT "The total of the two numbers is", total
- Stop
number1 = int(input("Enter your first number: "))
number2 = int(input("Enter your second number: "))
total = number1 + number2
print("The total of the two numbers is:", total)
Create a Flowchart & Python Program
Task: Create an algorithm that:
- Asks the user to input two numbers
- Adds the two numbers together and divides by 2 to find the mean average
- Outputs the average
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
average = (number1 + number2) / 2
print("The average is:", average)
Test Cases:
- number1 = 7, number2 = 9 → average should be 8
- number1 = 15, number2 = 20 → average should be 17.5
Essential Vocabulary
- Algorithm: A sequence of steps or instructions to solve a problem
- Flowchart: A diagram showing the sequence of actions in a computer program; a graphical representation to show the steps to solve a problem
- Syntax: The structure of the code used in a programming language
What is BIDMAS?
BIDMAS is an acronym to help you remember the order of operations when completing mathematical calculations. A program will carry out calculations in this order:
| Letter | Operation | Example |
|---|---|---|
| B | Brackets | (10 - 6) |
| I | Indices | 2³ = 2 × 2 × 2 = 8 |
| D | Division | 10 / 2 |
| M | Multiplication | 5 * 3 |
| A | Addition | 7 + 3 |
| S | Subtraction | 10 - 4 |
BIDMAS in Action
| Calculation | Outcome | Reason |
|---|---|---|
| 3 + 4 * 5 | 23 | 4 * 5 calculated first, then add 3 |
| 2 * 7 + 3 * 5 | 29 | 2*7 and 3*5 calculated first, then added |
| 6 + 3 * 9 / 11 | 8.45 | 9/11 first, then *3, then +6 |
| 20 * (10 - 6) / 5 | 16 | Brackets first (4), then *20, then /5 |
BIDMAS Practice Table
Complete the table by adding brackets to get the desired answer:
| Calculation | Answer | Desired Answer | Corrected Calculation |
|---|---|---|---|
| 5 * 8 + 6 | 46 | 70 | 5 * (8 + 6) |
| 35 - 10 + 15 | 10 | 40 | 35 - (10 + 15) or (35 - 10) + 15 |
| 9 + 3 * 5 | 24 | 60 | (9 + 3) * 5 |
| 9 * 8 - 20 / 5 | 68 | 10.4 | (9 * 8 - 20) / 5 |
| 60 * 40 - 3 * 48 | 2256 | 880 | 60 * (40 - 3) * 48 (needs review) |
| 18 * 6 - 2 / 8 | 107.75 | 9 | (18 * 6 - 2) / 8 |
Essential Vocabulary
- BIDMAS: An acronym to help you remember the order of operations: Brackets, Indices, Division, Multiplication, Addition, Subtraction
Test Plans
A test plan is used to check whether a program is working correctly. It contains tests showing what is expected to happen (expected outcome).
| Test # | Data Entered | Expected Outcome | Actual Outcome | Pass/Fail |
|---|---|---|---|---|
| 1 | num1=20, num2=15 | total = 35 | total = 300 | ❌ Fail |
| 2 | num1=19, num2=34 | total = 53 | total = 646 | ❌ Fail |
1. Missing Brackets
| Incorrect Code | Issue | Correct Code |
|---|---|---|
print("Hello World" |
Missing close bracket at end | print("Hello World") |
num1 = int(input("Enter a number:") |
Two open brackets, only one close | num1 = int(input("Enter a number:")) |
2. Missing Quotation Marks
| Incorrect Code | Issue | Correct Code |
|---|---|---|
name = input(Enter a name") |
Missing quotation mark before "Enter" | name = input("Enter a name") |
print("The name you entered was,name) |
Missing quotation mark after "was" | print("The name you entered was", name) |
print("The name you entered was,name") |
Quotation marks in wrong place | print("The name you entered was", name) |
How to Find and Fix Errors
- Read error messages carefully - Python tells you the line number and type of error
- Check brackets - Every opening bracket needs a closing bracket
- Check quotation marks - Every string needs matching quotes
- Check spelling - Python is case-sensitive (
printnotPrint) - Test with different inputs - Use a test plan to verify expected outcomes