Wednesday, September 30, 2026

Python Casting, int() float(), Python Flowcharts, BIDMAS Python, Order of Operations, Python Error Detection, Test Plans, ICT Programming Cambridge-Lower-Secondary-Computing-7-Ben-Barnes-Tristan-Kirkpatrick

🐍 Python: Casting, Flowcharts, BIDMAS & Errors

Data Types, Planning, Order of Operations & Debugging

📚 About this guide: Complete, exam-friendly notes for Advanced Python Programming. Master data type casting (int/float), create flowcharts and algorithms, understand BIDMAS order of operations, and learn error detection with test plans.
Concept 1

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.

⚠️ Important: The input() function captures user input as a string data type. If you want to perform calculations, you must cast it to int() or float().
Problem Example

Without Casting (Wrong Result)

num1 = input("Enter a number: ")
answer = num1 * 2
print("The answer is", answer)
          
❌ Output when entering 5: The answer is 55

Why? Python treats num1 as a string, so "5" * 2 becomes "55" (string repetition), not 10.

Solution

With Casting (Correct Result)

CodeWhat 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)
          
✅ Output when entering 5: The answer is 10
Keyword

Essential Vocabulary

  • Casting: The process of ensuring data is set to the correct data type
Exercise 1

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.")
          
✅ Corrected Solution:
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.")
            
Exercise 2

Rabbit Age Calculator

A rabbit's age is approximately 9 times the age of a human. Create a program that calculates rabbit age.

✅ Python Solution:
age = int(input("Enter the age of the rabbit: "))
rabbitAge = age * 9
print("The age of the rabbit in rabbit years is", rabbitAge)
            
Exercise 3

Square Area Calculator

Ask user for the length of one side of a square and calculate the area.

✅ Python Solution:
length = int(input("Enter the length of one side of a square: "))
area = length * length
print("The area of the square is:", area)
            
Exercise 4

Car Speed Calculator

Calculate speed using distance and time (Speed = Distance ÷ Time).

✅ Python Solution:
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.

Concept 2

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.

Symbols

Key Flowchart Symbols

SymbolShapePurpose
Start/StopOvalUsed at the start and end of the flowchart
ProcessRectangleUsed when something needs to happen (e.g., calculations)
Input/OutputParallelogramUsed when data needs to be input or output
Example

Flowchart → Python Code

Flowchart Steps:

  • Start
  • INPUT number1
  • INPUT number2
  • total = number1 + number2
  • OUTPUT "The total of the two numbers is", total
  • Stop
✅ Python Implementation:
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)
            
Practice

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
✅ Python Solution:
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
Keywords

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
Concept 3

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:

LetterOperationExample
BBrackets(10 - 6)
IIndices2³ = 2 × 2 × 2 = 8
DDivision10 / 2
MMultiplication5 * 3
AAddition7 + 3
SSubtraction10 - 4
⚠️ Important: Addition and subtraction are equal priority, so if both appear, they are calculated from left to right.
Examples

BIDMAS in Action

CalculationOutcomeReason
3 + 4 * 5234 * 5 calculated first, then add 3
2 * 7 + 3 * 5292*7 and 3*5 calculated first, then added
6 + 3 * 9 / 118.459/11 first, then *3, then +6
20 * (10 - 6) / 516Brackets first (4), then *20, then /5
Practice

BIDMAS Practice Table

Complete the table by adding brackets to get the desired answer:

CalculationAnswerDesired AnswerCorrected Calculation
5 * 8 + 646705 * (8 + 6)
35 - 10 + 15104035 - (10 + 15) or (35 - 10) + 15
9 + 3 * 52460(9 + 3) * 5
9 * 8 - 20 / 56810.4(9 * 8 - 20) / 5
60 * 40 - 3 * 48225688060 * (40 - 3) * 48 (needs review)
18 * 6 - 2 / 8107.759(18 * 6 - 2) / 8
Keyword

Essential Vocabulary

  • BIDMAS: An acronym to help you remember the order of operations: Brackets, Indices, Division, Multiplication, Addition, Subtraction
Concept 4

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 EnteredExpected OutcomeActual OutcomePass/Fail
1num1=20, num2=15total = 35total = 300❌ Fail
2num1=19, num2=34total = 53total = 646❌ Fail
🔍 Error Identified: The program is not working as expected. The arithmetic operator is wrong (likely using * instead of +).
Common Errors

1. Missing Brackets

Incorrect CodeIssueCorrect 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:"))
Common Errors

2. Missing Quotation Marks

Incorrect CodeIssueCorrect 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)
Debugging Tips

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 (print not Print)
  • Test with different inputs - Use a test plan to verify expected outcomes

❓ Frequently Asked Questions (FAQ)

Q: What is casting in Python? A: Casting is the process of ensuring data is set to the correct data type. In Python, the input() function captures data as a string, so you must cast it to int() for whole numbers or float() for decimal numbers before performing calculations.
Q: What is the difference between int() and float() in Python? A: int() converts data to an integer (whole number), while float() converts data to a floating-point number (decimal number). Use int() for counting and float() for measurements or calculations requiring decimals.
Q: What does BIDMAS stand for in programming? A: BIDMAS stands for Brackets, Indices, Division, Multiplication, Addition, Subtraction. It defines the order of operations in mathematical calculations. Addition and subtraction are equal priority and calculated left to right.
Q: What is a test plan in Python programming? A: A test plan is a document used to check whether a program is working correctly. It contains test numbers, data entered, expected outcomes, actual outcomes, and pass/fail status to identify and debug errors in the code.
Q: Why does "5" * 2 give "55" instead of 10 in Python? A: Because input() captures data as a string, "5" * 2 performs string repetition (repeating the string "5" twice), resulting in "55". To get 10, you must cast the input to int() first: int("5") * 2 = 10.

No comments:

Post a Comment