COS 202 - Computer Programming II
Module: Week 3 - Python Fundamentals & Control Flow Refresher
Motto for today: Building the foundation before we build the house.
By: Engr. Hassan Hamisu Ya’u
Maryam Abacha American University of Nigeria (MAAUN)
Variables & Data
Types (Storing
Information)
# Create your variables
Concept: Variables are containers for storing data values. candidate_name = "Aisha" # String
Python is dynamically typed, meaning you don't need to student_id = 10452 # Integer
declare the type upfront. test_score = 0.0 # Float
has_completed_test = False # Boolean
Let's Build: Setting up a candidate's profile for a CBT # Do it now: Print a welcome message using an f-string (formatted string) that
session. includes the candidate's name and ID.
print(f"Welcome {candidate_name} (ID: {student_id}) to the examination
portal.")
Control Flow
(if, elif, else)
score = 75
# Grading logic
if score >= 70:
grade = "A"
Concept: Conditional statements allow your program to make
elif score >= 60:
decisions and execute different blocks of code based on
grade = "B"
whether a condition is True or False. elif score >= 50:
grade = "C"
Let's Build: A grading mechanism. else:
grade = "F"
● Practice: Write a script that checks an answer and
updates a score, then determines the grade. # Do it now: Change the score variable to 45. Write an `if` statement that prints
"Please retake the module" ONLY if the grade is "F".
Loops
(for and while)
total_score = 0
# Iterating through the list of dictionaries
for q in test_bank:
print("\n" + q["prompt"])
for option in q["options"]:
print(option)
Concept: Loops allow you to repeat a block of code multiple
times. for loops iterate over a sequence (like a list), while
# user_answer = input("Enter your answer (A, B, or C): ")
while loops run as long as a condition remains true. # For practice, let's hardcode an answer instead of using input()
user_answer = "B"
if user_answer == q["answer"]:
Let's Build: Administering the test to the user. print("Correct!")
total_score += 1
● Practice: Iterate through your test_bank, display the
prompt, and ask for input. # Do it now: Write a simple `while` loop that acts as a 10-second timer, printing
● the seconds counting down from 10 to 0.
Functions
(Reusable Code Blocks)
def calculate_percentage(score, max_score):
"""Calculates the percentage and returns it."""
return (score / max_score) * 100
Concept: Functions group code into reusable blocks. They
def print_result(name, percentage):
take inputs (arguments), perform actions, and return outputs.
if percentage >= 50:
print(f"Congratulations {name}, you passed with {percentage}%!")
Let's Build: A reusable grading function.
else:
print(f"Sorry {name}, you failed with {percentage}%.")
● Practice: Wrap our earlier grading logic into a function
so we can use it for any student. # Do it now: Call calculate_percentage() with a score of 8 out of 10. Pass the
result into the print_result() function along with a candidate name.
Lab Challenge
(The Mini-CBT Engine)
Task: Build a fully functional Command Line Interface (CLI) quiz.
1. Setup: Create a list containing at least 3 dictionaries. Each dictionary must have a "question", "options", and
"correct_answer".
2. Execute: Write a for loop that iterates through the list.
3. Interact: Use Python's input() function to accept the user's answer from the terminal.
4. Evaluate: Use if/else statements inside the loop to check if the input() matches the "correct_answer". Keep
a running score.
5. Output: At the end of the loop, use a function to calculate their final percentage and print a pass/fail message.
Next Week: Advanced OOP & Program Organization in Python
Questions ?