2
AssignmentMultiple Choice

Creating a Game

Question 2 • SC-Fundamentals of Computing - Content Recovery - Design & Development

In your guessing game, you randomly chose a word from your word list. Complete the code to randomly choose an index found in the list. import random count = len(words) wordIndex = _____

Answer
A
random.randint(0,count - 1)
B
random.randint(0,count)
C
random.random(0,count - 1)
D
random.random(0,count)

Explanation

A list with count items has valid indices from 0 through count - 1. random.randint(a, b) can return either endpoint, so random.randint(0, count - 1) chooses a valid index. The choice random.randint(0, count) could return count, which is outside the list.

AI-written and checked against the verified answer.

Previous
Next