KnowledgeBoat Logo

Computer Science

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()
  1. DELHIDELHI
    MUMBAIMUMBAI
    CHENNAICHENNAI
    KOLKATAKOLKATA
  2. DELHI
    DELHIMUMBAI
    DELHIMUMBAICHENNAI
  3. DELHI
    MUMBAI
    CHENNAI
    KOLKATA
  4. DELHI
    MUMBAIMUMBAI
    KOLKATAKOLKATAKOLKATA

Python Libraries

6 Likes

Answer

DELHI
MUMBAI
CHENNAI
KOLKATA

DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA

The minimum value for PICK is 0 and the maximum value for PICK is 3.

Explanation
  1. import random — Imports the random module.
  2. PICK = random.randint(0, 3) — Generates a random integer between 0 and 3 (inclusive) and assigns it to the variable PICK.
  3. CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"] — Defines a list of cities.
  4. for I in CITY — This loop iterates over each city in the CITY.
  5. for J in range(1, PICK) — This loop will iterate from 1 up to PICK - 1. The value of PICK can be an integer in the range [0, 1, 2, 3]. For the cases when value of PICK is 0 or 1, the loop will not execute as range(1, 0) and range(1, 1) will return empty range.
    For the cases when value of PICK is 2 or 3, the names of the cities in CITY will be printed once or twice, respectively. Hence, we get the possible outcomes of this code.

Answered By

2 Likes


Related Questions