Session 5 - Functions, Nested Loops and Type Casting Flashcards
Functions, Nested Loops and Type Casting (8 cards)
Odd/Even function (1min)
Create a function to check whether a number is odd or even
Input:
Enter a number: 7
Output:
Odd
A
Maximum Getter function (1min)
Print the Sum of All Elements using a function
Input:
Enter numbers: 3 9 4 2
Output:
Max = 9
A
Multiplication Table (2mins)
Print a Table from 1 to 5 with only five elements per row using nested loops
Input:
Enter number of columns: 5
Output:
1 2 3 4 5
2 4 6 8 10
…
5 10 15 20 25
A
String to Int (2mins)
Using a function, convert String Input to Integer and Multiply
Input:
Enter a number: 5
Output:
Square = 25
Answer:
num = int(input(“Enter a number: “))
print(“Square =”, num * num)
A
Float to Int (2mins)
Using a function, round Float to Integer
Input:
Enter a float: 7.9
Output:
Rounded = 8
A
Second Largest (2mins)
Find the Second Largest Number
Input:
Enter numbers: 4 1 7 7 3
Output:
Second largest: 4
A
Boolean to Int (2mins)
Cast Boolean to Integer
Input:
Enter a number: 0
Output:
Boolean value: False
As integer: 0
A
Grade Calculator (4mins)
Using functions, nested loops and type casting, write a program that takes the marks of students in multiple subjects and prints a formatted grade report.
a. Input the number of students and subjects.
b. Input each student’s marks (space-separated, one line per student).
c. Use a function to calculate the average of each student.
d. Print:
* Student number
* Their marks
* Their average (rounded to 2 decimal places)
* “Pass” if average ≥ 40, otherwise “Fail”
Input:
Enter number of students: 2
Enter number of subjects: 3
Enter marks for student 1: 80 70 60
Enter marks for student 2: 30 35 20
Output:
Student 1: Marks = [80, 70, 60], Average = 70.0, Result = Pass
Student 2: Marks = [30, 35, 20], Average = 28.33, Result = Fail
A