CHAPTER PDF
Complete curriculum textbook chapter document for reading and offline study.
NOTES
Complete conceptual breakdown, formulas, diagrams, and memory aids.
Flow of Control
Complete chapter notes covering decision-making, indentation, repetition, for and while loops, range(), break, continue, nested loops, worked programs and textbook exercise solutions.
Learning outcomes
After studying this chapter, learners should be able to control the order in which Python statements execute. You will learn to make decisions using conditions, repeat instructions using loops, control loop execution with break and continue, and solve problems using nested loops.
Three fundamental flow patterns
Sequence: statements run one after another. Selection: a condition determines which block runs. Repetition: a block runs repeatedly while a loop requires it.
6.1 Introduction to Flow of Control
The flow of control is the order in which statements in a program are executed. In a simple sequential program, Python executes statements from top to bottom, in the order in which they are written.
The chapter compares this with a bus travelling along a road to school: it follows the route milestone by milestone. This illustrates sequence, the basic execution pattern introduced in the previous chapter.
Example 6-1: Difference between two numbers (sequential execution)
# Accept two numbers and display their difference
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
diff = num1 - num2
print("The difference of", num1, "and", num2, "is", diff)
Sample run:
Enter first number: 5
Enter second number: 7
The difference of 5 and 7 is -2
Every statement executes in sequence. The result can be negative because the program always subtracts the second number from the first.
6.2 Selection (Decision Making)
Selection means choosing one course of action from two or more possible alternatives based on a condition. Everyday examples include selecting a pen within a budget or choosing a route based on shortest distance or traffic.
In Python, selection is implemented using if, if...else and if...elif...else statements.
Flow of a decision
A condition is an expression that evaluates to True or False, usually using relational or logical operators.
6.2.1 The if Statement
The if statement executes its indented block only when its condition is true. If the condition is false, the block is skipped and execution continues after it.
Syntax
if condition:
statement_1
statement_2
# next statement outside the if block
The colon (:) is compulsory. The statements controlled by the condition must be indented consistently.
Example 6-1: Voting eligibility
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
print("Program completed")
If the user enters 20, both messages are printed. If the user enters 16, only Program completed is printed. The condition age >= 18 includes age 18.
age >= 18. The latter correctly implements eligibility from age 18 inclusive.Positive difference using if–else
To always subtract the smaller number from the larger number, compare the inputs first.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
diff = num1 - num2
else:
diff = num2 - num1
print("Positive difference:", diff)
For inputs 5 and 6, the output is Positive difference: 1.
6.2.2 The if...else and if...elif...else Statements
if...else syntax
if condition:
statement(s)_when_true
else:
statement(s)_when_false
Exactly one of the two blocks is executed. The else block has no condition; it handles every case in which the if condition is false.
if...elif...else syntax
if condition_1:
statement(s)_1
elif condition_2:
statement(s)_2
elif condition_3:
statement(s)_3
else:
default_statement(s)
elif means “else if”. Python checks conditions from top to bottom. The first true condition's block executes, and the remaining branches are skipped. The final else is optional and acts as a default branch.
Example 6-2: Positive, negative or zero
number = int(input("Enter a number: "))
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")
Test values: 12 → positive; −5 → negative; 0 → zero.
Example 6-3: Traffic signal message
signal = input("Enter the colour: ").strip().lower()
if signal == "red":
print("STOP")
elif signal == "orange":
print("Be Slow")
elif signal == "green":
print("Go!")
else:
print("Invalid signal colour")
The textbook handles lowercase and uppercase spellings with separate comparisons such as signal == "red" or signal == "RED". Using .lower() is a shorter Python 3 approach that also handles mixed case such as Red.
Difference between else and elif
| else | elif |
|---|---|
| Does not contain a condition. | Must be followed by a condition. |
| Runs when all preceding conditions are false. | Runs only when its own condition is true and earlier conditions were false. |
| At most one final else branch. | One or more elif branches may be used. |
Nested if and the Four-Function Calculator
A nested if is an if or if...else statement placed inside another selection block. It is useful when a decision requires a second decision.
Example 6-4: Simple four-function calculator
Requirements from the chapter:
- Accept two numbers and an operator (
+,-,*or/). - For subtraction, display the positive difference.
- For division, prevent division by zero.
- Display an error for an unsupported operator.
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter operator (+, -, *, /): ").strip()
if op == "+":
result = val1 + val2
print("The result is", result)
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
print("The positive difference is", result)
elif op == "*":
result = val1 * val2
print("The result is", result)
elif op == "/":
if val2 == 0:
print("Error: division by zero is not allowed")
else:
result = val1 / val2
print("The result is", result)
else:
print("Wrong operator entered")
Sample run: 84, 4 and / gives The result is 21.0.
if checks whether the divisor is zero. Inside subtraction, another decision selects the correct order to ensure a non-negative difference.6.3 Indentation
Indentation is the leading whitespace (spaces or tabs) at the beginning of a statement. Python uses indentation to identify a block and nested blocks. Unlike languages that use curly brackets to delimit blocks, Python uses indentation as part of its syntax.
Rules of indentation
- Statements belonging to the same block must have the same indentation level.
- A nested block must be indented further than the block containing it.
- Use a consistent indentation style. Four spaces per level is the widely used Python convention.
- Do not mix tabs and spaces inconsistently.
- A colon introduces a suite/block in constructs such as
if,else,forandwhile.
Example 6-5: Larger of two pre-specified numbers
num1 = 5
num2 = 6
if num1 > num2:
print("First number is larger")
print("Bye")
else:
print("Second number is larger")
print("Bye Bye")
Output:
Second number is larger
Bye Bye
Both print statements in each branch are indented equally and therefore belong to that branch.
else is indented inside the if block instead of aligning with if, Python raises a syntax/indentation error.6.4 Repetition (Iteration / Looping)
Repetition means executing a set of statements repeatedly. It is also called iteration. Real-life examples include paying an electricity bill every month and the butterfly life cycle (egg → caterpillar → pupa → butterfly → cycle repeats).
Writing thousands of separate print statements is inefficient. A loop performs repeated work using a compact block of code.
Loop control variable
A loop commonly uses a control variable whose value changes during each iteration. A condition determines whether the loop should continue. The programmer must ensure that the loop eventually terminates unless an intentionally infinite loop is required.
Python provides two principal looping constructs: for and while.
| for loop | while loop |
|---|---|
| Iterates over items in a sequence or range. | Repeats while a condition remains true. |
| Useful when the items or number of iterations are known. | Useful when repetition depends on a condition or input. |
| Automatically advances to the next item. | Programmer generally updates the control variable. |
6.4.1 The for Loop
The for statement iterates over a sequence of values. On each iteration, the loop variable receives the next item. The loop ends when all items have been processed.
Syntax
for control_variable in sequence:
statement(s) inside loop body
# statement after the loop
The sequence may be a string, list, tuple or a sequence produced by range(). The body must be indented.
Example 6-6: Print characters of PYTHON
for letter in "PYTHON":
print(letter)Output: P, Y, T, H, O and N, each on a separate line.
Example 6-7: Print values in a sequence
count = [10, 20, 30, 40, 50]
for num in count:
print(num)
Example 6-8: Print even numbers in a sequence
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num % 2 == 0:
print(num, "is an even number")
Logic: An even integer has remainder zero when divided by 2. The expression num % 2 == 0 tests this property.
The range() Function
range() is a built-in Python function that generates an arithmetic progression of integers. It is frequently used with a for loop.
Syntax
range(stop)
range(start, stop)
range(start, stop, step)
| Parameter | Meaning | Default / rule |
|---|---|---|
start | First integer in the sequence. | Default is 0. |
stop | Boundary at which generation stops. | Required; stop is excluded. |
step | Increment or decrement between values. | Default is 1; cannot be 0. |
Worked range examples
list(range(10))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(2, 10))
# [2, 3, 4, 5, 6, 7, 8, 9]
list(range(0, 30, 5))
# [0, 5, 10, 15, 20, 25]
list(range(0, -9, -1))
# [0, -1, -2, -3, -4, -5, -6, -7, -8]
In Python 3, range() returns a range object rather than a list. list(range(...)) is used above to display all generated values.
Example 6-9: Multiples of 10
for num in range(5):
if num > 0:
print(num * 10)
Output: 10, 20, 30, 40. The range is 0 through 4; the condition skips 0.
More useful range patterns
# 1 to 10 inclusive
for i in range(1, 11):
print(i)
# Even numbers from 2 to 20
for i in range(2, 21, 2):
print(i)
# Countdown from 5 to 1
for i in range(5, 0, -1):
print(i)
range(1, 11), not range(1, 10).6.4.2 The while Loop
The while loop repeatedly executes its body as long as its test condition is true. The condition is checked before the body on every iteration. Therefore, if the condition is false initially, the body does not execute even once.
Syntax
initialisation
while test_condition:
statement(s) inside loop
update_control_variable
# statement after the loop
Three essential parts
- Initialisation: Give the control variable its starting value.
- Condition: Decide whether another iteration should execute.
- Update: Change the control variable so that the condition can eventually become false.
Example 6-10: First five natural numbers
count = 1
while count <= 5:
print(count)
count += 1
Output: 1, 2, 3, 4, 5. After printing 5, count becomes 6, the condition is false and the loop terminates.
Example 6-11: Find factors of a whole number
num = int(input("Enter a positive integer: "))
if num <= 0:
print("Please enter a positive integer")
else:
print(1, end=" ")
factor = 2
while factor <= num // 2:
if num % factor == 0:
print(factor, end=" ")
factor += 1
print(num)
For input 6, output is 1 2 3 6. A factor divides the number exactly, so the remainder is zero. 1 and the number itself are always factors of a positive integer.
Infinite loop
An infinite loop is a loop that never reaches its terminating condition. It may happen when the condition never becomes false, for example because the control variable is not updated correctly.
# Infinite loop – do not run without a stopping mechanism
count = 1
while count <= 5:
print(count)
# Missing count += 1
Correct version:
count = 1
while count <= 5:
print(count)
count += 1
6.5 break and continue Statements
These statements provide additional control over loops. They are different: break terminates the current loop, while continue skips the remaining statements in the current iteration and starts the next iteration.
6.5.1 break Statement
The break statement immediately exits the nearest enclosing loop. Execution resumes at the first statement after that loop.
Syntax
for/while condition:
statements
if stopping_condition:
break
remaining_statements
Example 6-12: Stop when the counter reaches 8
for num in range(10):
num = num + 1
if num == 8:
break
print("Num has value", num)
print("Encountered break!! Out of loop")
Output displays values 1 through 7. When num becomes 8, break terminates the loop and the final message is printed.
Example 6-13: Sum positive numbers until a negative number is entered
total = 0
print("Enter numbers; a negative number ends input.")
while True:
entry = int(input("Number: "))
if entry < 0:
break
total += entry
print("Sum =", total)
Sample inputs 3, 4, 5, −1 produce Sum = 12. The negative sentinel value stops input and is not added to the sum.
Example 6-14: Check whether a number is prime
A prime number is an integer greater than 1 that has exactly two positive factors: 1 and itself. If any divisor other than 1 and itself is found, the number is not prime and the search can stop early.
num = int(input("Enter a number: "))
if num <= 1:
print("Numbers less than or equal to 1 are not prime")
else:
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
This version checks divisors only up to the square root of the number, which is sufficient and more efficient than checking all values up to half the number. The textbook's original example checks up to approximately half the number.
6.5.2 continue Statement
The continue statement skips the remaining statements in the current iteration and proceeds to the next iteration. It does not terminate the loop.
Syntax
for/while condition:
statements
if skip_condition:
continue
statements_to_skip_when_condition_is_true
Example 6-15: Print numbers except 3
for num in range(1, 7):
if num == 3:
continue
print("Num has value", num)
print("End of loop")
Output displays 1, 2, 4, 5, 6 and then End of loop. The value 3 is skipped; the loop continues.
break vs continue
| break | continue |
|---|---|
| Terminates the nearest enclosing loop. | Skips only the current iteration. |
| Control moves to the statement after the loop. | Control moves to the next iteration. |
| Used when no further loop processing is required. | Used when one particular iteration should be ignored. |
6.6 Nested Loops
A nested loop is a loop placed inside another loop. The outer loop controls the number of times the inner loop is started. For each outer iteration, the inner loop completes all its iterations unless it is interrupted by a control statement.
Python allows for loops inside for loops, while loops inside for loops, and other combinations.
Example 6-16: Nested for loops
for var1 in range(3):
print("Iteration", var1 + 1, "of outer loop")
for var2 in range(2):
print(var2 + 1)
print("Out of inner loop")
print("Out of outer loop")
The inner loop prints 1 and 2 for every outer iteration. Since the outer loop runs 3 times and the inner loop runs 2 times per outer iteration, the inner body runs 3 × 2 = 6 times.
Example 6-17: Number triangle pattern
For input 5, the required output is:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
num = int(input("Enter number of rows: "))
for i in range(1, num + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
Explanation: The outer loop selects the row number. The inner loop prints values from 1 up to that row number. The empty print() moves to the next line after each row.
Example 6-18: Prime numbers between 2 and 50
for num in range(2, 51):
is_prime = True
for divisor in range(2, int(num ** 0.5) + 1):
if num % divisor == 0:
is_prime = False
break
if is_prime:
print(num, "is a prime number")
print("Bye Bye!!")
Output prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 and 47. The outer loop selects each candidate; the inner loop searches for a divisor.
Example 6-19: Factorial of a number
The factorial of a non-negative integer n is the product of all positive integers from 1 to n. It is written as n!. By definition, 0! = 1.
num = int(input("Enter a number: "))
fact = 1
if num < 0:
print("Factorial is not defined for negative integers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
fact *= i
print("Factorial of", num, "is", fact)
For input 5: 1 × 2 × 3 × 4 × 5 = 120.
Chapter Summary and Revision Notes
- Flow of control is the order in which program statements execute.
- Selection is performed using
if,if...elseandif...elif...else. - Indentation defines blocks and nested blocks in Python.
- Repetition (iteration) is implemented using
forandwhile. - A
forloop iterates over a sequence or range. range(start, stop, step)generates integers; stop is excluded and step cannot be zero.- A
whileloop repeats while its condition is true. Its body may execute zero times if the condition is initially false. - A while loop needs a valid update or exit condition to avoid an unintended infinite loop.
breakexits the nearest loop;continueskips the rest of the current iteration.- A loop inside another loop is called a nested loop.
Quick syntax reference
# if
if condition:
statements
# if-else
if condition:
statements
else:
statements
# if-elif-else
if condition1:
statements
elif condition2:
statements
else:
statements
# for
for item in sequence:
statements
# while
while condition:
statements
update
# break / continue
if condition:
break
if condition:
continue
Textbook Exercise – Conceptual Questions and Output
Q1. Difference between else and elif
elif introduces another condition that is tested only when all earlier conditions are false. else has no condition and executes only when every preceding condition is false. Multiple elif branches are allowed; there can be at most one final else.
Q2. Purpose of range() with example
range() generates a sequence of integers, commonly for use in a for loop. Its forms are range(stop), range(start, stop) and range(start, stop, step).
for i in range(1, 6):
print(i)Output: 1, 2, 3, 4, 5. The stop value 6 is excluded.
Q3. Differentiate between break and continue
break immediately terminates the nearest loop. continue skips the remaining statements of the current iteration and continues with the next iteration.
# break: prints 1, 2, then exits
for i in range(1, 5):
if i == 3:
break
print(i)
# continue: prints 1, 2, 4
for i in range(1, 5):
if i == 3:
continue
print(i)Q4. What is an infinite loop? Give an example.
An infinite loop is a loop that does not terminate because its stopping condition is never reached or its condition always remains true.
i = 1
while i <= 5:
print(i)
# i is never incremented: infinite loop
Correction: add i += 1 inside the loop.
Q5. Find the output of the program segments
(i)
a = 110
while a > 100:
print(a)
a -= 2Output:
110
108
106
104
102When a becomes 100, a > 100 is false and the loop stops.
(ii)
for i in range(20, 30, 2):
print(i)Output:
20
22
24
26
28(iii)
country = 'INDIA'
for i in country:
print(i)Output:
I
N
D
I
A(iv)
i = 0
sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print(sum)Values of i are 0, 2, 4, 6, 8. Values divisible by 4 are 0, 4 and 8. Their sum is 12.
Output: 12
(v)
for x in range(1, 4):
for y in range(2, 5):
if x * y > 10:
break
print(x * y)Output:
2
3
4
4
6
8
6
9For x=1, products are 2, 3, 4. For x=2, products are 4, 6, 8. For x=3, products are 6, 9, 12; 12 is greater than 10, so the inner loop breaks before printing it.
(vi)
var = 7
while var > 0:
print('Current variable value: ', var)
var = var - 1
if var == 3:
break
else:
if var == 6:
var = var - 1
continue
print("Good bye!")Output:
Current variable value: 7
Current variable value: 5
Current variable value: 4After printing 7, var becomes 6; the inner condition reduces it to 5 and continue skips “Good bye!”. Then 5 is printed, var becomes 4 and “Good bye!” is printed. Next 4 is printed, var becomes 3 and break exits before the goodbye message.
Programming Exercises – Complete Worked Solutions
These solutions cover all ten programming exercises on textbook pages 140–141. Programs use Python 3 and include input handling and clear variable names.
1. Driving licence eligibility (minimum age 18)
name = input("Enter your name: ")
age = int(input("Enter your age: "))
if age >= 18:
print(name, "is eligible to apply for a driving licence")
else:
print(name, "is not eligible to apply for a driving licence")2. Function to print the multiplication table
def print_table(number):
for i in range(1, 11):
print(number, "x", i, "=", number * i)
n = int(input("Enter a number: "))
print_table(n)The function receives a number and prints its products from 1 to 10.
3. Minimum and maximum of five numbers
numbers = []
for i in range(5):
value = float(input("Enter number: "))
numbers.append(value)
print("Minimum =", min(numbers))
print("Maximum =", max(numbers))Alternative without built-in min/max: initialise both values from the first input and update them while reading the remaining values.
4. Check whether a year is a leap year
A year is a leap year if it is divisible by 400, or divisible by 4 but not divisible by 100.
year = int(input("Enter year: "))
if year % 400 == 0:
print("Leap year")
elif year % 100 == 0:
print("Not a leap year")
elif year % 4 == 0:
print("Leap year")
else:
print("Not a leap year")5. Generate sequence −5, 10, −15, 20, −25 ... up to n terms
Interpret n as the number of terms to generate. Odd-position terms are negative and even-position terms are positive; magnitudes are multiples of 5.
n = int(input("Enter number of terms: "))
for i in range(1, n + 1):
term = 5 * i
if i % 2 == 1:
term = -term
print(term, end=" ")For n=5: -5 10 -15 20 -25.
6. Sum 1 + 1/8 + 1/27 + ... + 1/n³
n = int(input("Enter n: "))
if n <= 0:
print("Enter a positive integer")
else:
total = 0
for i in range(1, n + 1):
total += 1 / (i ** 3)
print("Sum =", total)Each term is 1 / i³, and the loop includes i=n.
7. Find the sum of digits of an integer
number = int(input("Enter an integer: "))
number = abs(number)
digit_sum = 0
while number > 0:
digit = number % 10
digit_sum += digit
number //= 10
print("Sum of digits =", digit_sum)For 527: 7 + 2 + 5 = 14. The % 10 operation extracts the last digit; // 10 removes it. For input 0, the sum is 0.
8. Function to check a number palindrome
A palindrome reads the same forwards and backwards. Examples: 12321 and 4554.
def is_palindrome(number):
original = str(number)
reversed_value = original[::-1]
return original == reversed_value
value = input("Enter a number: ")
if is_palindrome(value):
print("Palindrome")
else:
print("Not a palindrome")String comparison also works for the textbook's definition that a number or string may be a palindrome. If negative integers are considered, the minus sign makes them non-palindromic.
9(i). Print the diamond-like star pattern
for stars in (1, 3, 5, 3, 1):
print("*" * stars)Output:
*
***
*****
***
*9(ii). Print the number pyramid
for row in range(1, 6):
for value in range(row, 0, -1):
print(value, end="")
for value in range(2, row + 1):
print(value, end="")
print()Output:
1
212
32123
4321234
5432123459(iii). Right-aligned descending number pattern
for row in range(5, 0, -1):
print(" " * (5 - row), end="")
for value in range(1, row + 1):
print(value, end="")
print()Output:
12345
1234
123
12
19(iv). Hollow diamond / outline star pattern
n = 3
for row in range(n):
if row == 0:
print(" " * n + "*")
else:
print(" " * (n - row) + "*" + " " * (2 * row - 1) + "*")
for row in range(n - 2, -1, -1):
if row == 0:
print(" " * n + "*")
else:
print(" " * (n - row) + "*" + " " * (2 * row - 1) + "*")This prints a hollow diamond outline. The exact spacing can be adjusted to match the textbook's printed pattern in a monospace font.
10. Assign grade from percentage
To avoid overlapping ranges, check from highest to lowest. The textbook table uses “Above 90%” for A, 80% to 90% for B, 70% to 80% for C, 60% to 70% for D and below 60% for E. The inclusive boundary values 80, 70 and 60 belong to the lower band as shown by the descending conditions below.
percentage = float(input("Enter percentage: "))
if percentage > 90:
grade = "A"
elif percentage >= 80:
grade = "B"
elif percentage >= 70:
grade = "C"
elif percentage >= 60:
grade = "D"
else:
grade = "E"
print("Grade:", grade)Examples: 95 → A; 90 → B; 80 → B; 75 → C; 65 → D; 59 → E.
Case Study – Student Management Information System (SMIS)
This chapter extends the Student Management Information System developed in Chapter 5. The menu-driven program should:
- Accept marks in five major Class X subjects and display them.
- Calculate the total marks.
- Calculate percentage as total marks divided by 5 (assuming each subject is out of 100).
- Display the grade according to the supplied criteria.
Grade criteria from the textbook
| Percentage | Grade |
|---|---|
| Greater than 85 | A |
| 75 to less than 85 | B |
| 50 to less than 75 | C |
| Greater than 30 to 50 inclusive | D |
| Less than 30 | Reappear |
Complete menu-driven program
marks = []
total = 0
percentage = 0
while True:
print("\\nSTUDENT MANAGEMENT SYSTEM")
print("1. Enter marks in five subjects")
print("2. Display marks")
print("3. Calculate total and percentage")
print("4. Display grade")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
marks = []
for subject in range(1, 6):
score = float(input("Enter marks for subject " + str(subject) + ": "))
if score < 0 or score > 100:
print("Marks must be between 0 and 100.")
marks = []
break
marks.append(score)
elif choice == "2":
if len(marks) == 5:
for index, score in enumerate(marks, start=1):
print("Subject", index, ":", score)
else:
print("Please enter all five subject marks first.")
elif choice == "3":
if len(marks) == 5:
total = sum(marks)
percentage = total / 5
print("Total marks:", total, "out of 500")
print("Percentage:", percentage, "%")
else:
print("Please enter all five subject marks first.")
elif choice == "4":
if len(marks) != 5:
print("Please enter all five subject marks first.")
else:
total = sum(marks)
percentage = total / 5
if percentage > 85:
grade = "A"
elif percentage >= 75:
grade = "B"
elif percentage >= 50:
grade = "C"
elif percentage > 30:
grade = "D"
elif percentage < 30:
grade = "Reappear"
else:
grade = "Boundary clarification required (30%)"
print("Percentage:", percentage, "%")
print("Grade:", grade)
elif choice == "5":
print("Thank you. Exiting SMIS.")
break
else:
print("Invalid choice. Please select 1 to 5.")
Program explanation
- The
while Trueloop keeps displaying the menu until the user chooses Exit. - The
if...elif...elseselection processes the menu option. - A
forloop accepts exactly five subject marks. - The program validates that each mark is between 0 and 100.
sum(marks)calculates the total; dividing by five gives the percentage when each subject is out of 100.- The grade is selected by checking percentage bands from highest to lowest.
breakexits the menu loop when option 5 is selected.
Documentation and peer review
The case study asks learners to peer-review one another's programs using the documentation tips from Chapter 5. Check that the program has a clear objective, meaningful variable names, suitable comments, consistent indentation, readable structure and understandable output.
TEXTBOOK SOLUTION
Step-by-step solved textbook exercises, derivations, and solutions.
Exercise Solutions Being Updated
Step-by-step verified exercise solutions are currently being uploaded.
IMPORTANT QUESTION
High-yield board questions, previous year questions (PYQs), and model answer keys.
Important Questions Being Compiled
Board blueprint and previous years' exam questions for this chapter will be live soon.