0% found this document useful (0 votes)
0 views4 pages

Nested Loop Random Worksheet

The document contains a series of programming questions related to nested loops and the random module in Python. Each question requires determining the minimum and maximum number of times a name or value can be printed per line, as well as the total number of lines printed. The answers are provided for each question, detailing the logic behind the calculations.

Uploaded by

Anshul Chand
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Nested Loop Random Worksheet

The document contains a series of programming questions related to nested loops and the random module in Python. Each question requires determining the minimum and maximum number of times a name or value can be printed per line, as well as the total number of lines printed. The answers are provided for each question, detailing the logic behind the calculations.

Uploaded by

Anshul Chand
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Class XII — Computer Science / IP

Nested Loops with random Module — Predict Min/Max Output


For each program, find the minimum and maximum number of times a name/value can be printed per line, and
state how many lines are printed in total.

Q1.

import random
SEL = [Link](0, 3)
FRUITS = ["Apple", "Mango", "Guava"]
for F in FRUITS:
for X in range(0, SEL):
print(F, end="")
print()

Find the minimum and maximum number of times a name can be printed per line, and describe how many lines are
printed in total.

Q2.

import random
ANIMAL = ["Cat", "Dog", "Lion", "Tiger"]
for A in ANIMAL:
SEL = [Link](1, 3)
for X in range(SEL):
print(A, end="")
print()

Find the minimum and maximum number of times a name can be printed per line, and describe how many lines are
printed in total.

Q3.

import random
SEL = [Link](0, 2)
COLORS = ["Red", "Blue"]
for C in COLORS:
for X in range(SEL, 3):
print(C, end="")
print()

Find the minimum and maximum number of times a name can be printed per line, and describe how many lines are
printed in total.
Q4.

import random
SEL = [Link](2, 5)
ITEMS = ["Pen", "Book"]
for I in ITEMS:
for X in range(SEL, 0, -1):
print(I, end="")
print()

Find the minimum and maximum number of times a name can be printed per line, and describe how many lines are
printed in total.

Q5.

import random
SEL = [Link](1, 8, 2)
NAMES = ["Ravi", "Sita"]
for N in NAMES:
for X in range(0, SEL):
print(N, end="*")
print()

Find the minimum and maximum number of times a name can be printed per line, and describe how many lines are
printed in total.

Q6.

import random
SEL = [Link](1, 4)
BIRDS = ["Crow", "Parrot", "Sparrow"]
for B in BIRDS:
for X in range(1, SEL + 1):
if X % 2 == 0:
print(B, end="")
print()

Find the minimum and maximum number of times a name can be printed per line, and describe how many lines are
printed in total.
Q7.

import random
SEL = [Link](0, 3)
CARS = ["Audi", "BMW", "Kia", "Ford"]
for C in CARS:
for X in range(SEL, 4):
print(C, end="#")
print()

Find the minimum and maximum number of times a name can be printed per line, and describe how many lines are
printed in total.

Q8.

import random
SEL = [Link](1, 4)
VEG = ["Peas", "Corn", "Beans", "Carrot"]
for V in VEG[:SEL]:
for X in range(2):
print(V, end="")
print()

Find the minimum and maximum number of times a name can be printed per line, and describe how many lines are
printed in total.
Answer Key
Q1. SEL is picked once, before the loops start, so it is the same value for every fruit. SEL in {0,1,2,3}. Inner loop
range(0, SEL) has length SEL, so each fruit name is printed SEL times on its line. Minimum repeats per line = 0
(SEL=0, all 3 lines blank). Maximum repeats per line = 3 (SEL=3, each name printed 3 times). Total lines printed =
3 (one per fruit) always.

Q2. Here SEL is generated fresh inside the outer loop, so each animal can get a different, independent repeat count.
SEL in {1,2,3} each time. Minimum repeats per line = 1, maximum repeats per line = 3. The 4 lines need not match
each other. Total lines printed = 4 (one per animal).

Q3. SEL in {0,1,2}, fixed once outside the loop. Inner loop range(SEL, 3) has length 3-SEL, giving lengths 3, 2, 1.
Minimum repeats per line = 1 (SEL=2), maximum repeats per line = 3 (SEL=0). Both colour lines use the same
SEL, so both lines repeat the same number of times. Total lines printed = 2.

Q4. SEL in {2,3,4,5}, fixed once outside the loop. Inner loop range(SEL, 0, -1) has length SEL. Minimum repeats
per line = 2 (SEL=2), maximum repeats per line = 5 (SEL=5). Both item lines repeat the same number of times.
Total lines printed = 2.

Q5. [Link](1, 8, 2) can only give odd values: 1, 3, 5, 7. Inner loop range(0, SEL) has length SEL, so
each name is printed SEL times, separated by '*'. Minimum repeats per line = 1, maximum repeats per line = 7.
Both name lines repeat the same number of times. Total lines printed = 2.

Q6. SEL in {1,2,3,4}, fixed once outside the loop. Inner loop range(1, SEL+1) produces the numbers 1..SEL; the
bird name is printed only when that number is even. Number of even values: SEL=1 -> 0, SEL=2 -> 1, SEL=3 -> 1,
SEL=4 -> 2. Minimum repeats per line = 0 (SEL=1), maximum repeats per line = 2 (SEL=4). All 3 bird lines
behave identically since SEL is fixed once. Total lines printed = 3.

Q7. SEL in {0,1,2,3}, fixed once outside the loop. Inner loop range(SEL, 4) has length 4-SEL, giving lengths 4, 3,
2, 1. Minimum repeats per line = 1 (SEL=3), maximum repeats per line = 4 (SEL=0). All 4 car lines repeat the same
number of times. Total lines printed = 4.

Q8. SEL in {1,2,3,4} controls how many vegetables are looped over, via the slice VEG[:SEL] - it does NOT affect
the inner loop. The inner loop range(2) is fixed, so every vegetable that is printed appears exactly twice on its own
line. Minimum lines printed = 1 (SEL=1), maximum lines printed = 4 (SEL=4). Repeats per line = always exactly
2.

You might also like