CBSE Class 12 Computer Science
Question 58 of 62
Using Python Libraries — Question 11
Back to all questions 11
Question What are the possible outcome(s) executed from the following code ? Also specify the maximum and minimum values that can be assigned to variable PICK.
import random
PICK = random.randint(0, 3)
CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"];
for I in CITY :
for J in range(1, PICK):
print(I, end = " ")
print()- DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA - DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI - DELHI
MUMBAI
CHENNAI
KOLKATA - DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA
DELHI
MUMBAI
CHENNAI
KOLKATA
DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA
The minimum value for PICK is 0 and the maximum value for PICK is 3.
import random— Imports therandommodule.PICK = random.randint(0, 3)— Generates a random integer between 0 and 3 (inclusive) and assigns it to the variablePICK.CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]— Defines a list of cities.for I in CITY— This loop iterates over each city in theCITY.for J in range(1, PICK)— This loop will iterate from 1 up to PICK - 1. The value ofPICKcan be an integer in the range [0, 1, 2, 3]. For the cases when value ofPICKis 0 or 1, the loop will not execute asrange(1, 0)andrange(1, 1)will return empty range.
For the cases when value ofPICKis 2 or 3, the names of the cities inCITYwill be printed once or twice, respectively. Hence, we get the possible outcomes of this code.