मराठी

What possible output from the given options is expected to be displayed when the following code is executed? import random Cards=["Heart", "Spade", "Club", "Diamond"] for i in range (2): print - Computer Science (Python)

Advertisements
Advertisements

प्रश्न

What possible output from the given options is expected to be displayed when the following code is executed?

import random
Cards=["Heart", "Spade", "Club", "Diamond"]
for i in range (2):
    print(Cards[random.randint(1,i+2) ],end="#")

पर्याय

  • Spade#Diamond#

  • Spade#Heart#

  • Diamond#Club#

  • Heart#Spade#

MCQ
Advertisements

उत्तर

Spade#Diamond#

Explanation:

The range(2) function generates the values 0 and 1, so the loop runs twice with i = 0 and i = 1.

  • In the first iteration (i = 0), random.randint(1, i+2) becomes random.randint(1, 2), which can select index 1 or 2, corresponding to “Spade” or “Club”.
  • In the second iteration (i = 1), random.randint(1, i+2) becomes random.randint(1, 3), which can select the index 1, 2, or 3, corresponding to “Spade”, “Club”, or “Diamond”.

Hence, Spade#Diamond# is a possible output.

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2024-2025 (March) Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×