If A=random.randint(B, C) assigns a random value between 1 and 6 (both…
2023
If A=random.randint(B, C) assigns a random value between 1 and 6 (both inclusive) to the identifier A, what should be the values of B and C, if all required modules have already been imported?
Answer: D. B=1, C=6 — Concept: Python's random.randint(a, b) function returns a random integer N such that a <= N <= b — both endpoints of the range you pass in are included among…
- A.
B=0, C=6
- B.
B=0, C=7
- C.
B=1, C=7
- D.
B=1, C=6
Attempted by 1412 students.
Show answer & explanation
Correct answer: D
Concept: Python's random.randint(a, b) function returns a random integer N such that a <= N <= b — both endpoints of the range you pass in are included among the possible results (unlike randrange, whose stop value is excluded).
Application: The identifier A must hold a value between 1 and 6, both inclusive. To make random.randint(B, C) produce exactly that closed range, the arguments must match the range's own endpoints:
The lowest value A can take is 1, so the first argument B must equal 1 (the inclusive lower endpoint).
The highest value A can take is 6, so the second argument C must equal 6 (the inclusive upper endpoint).
With B=1 and C=6, the call random.randint(1, 6) returns an integer N with 1 <= N <= 6, exactly the range required for A.
Cross-check: An equivalent call using the stop-exclusive random.randrange(1, 7) would need to go one past 6 to include it, confirming that randint's own upper bound must sit at 6, not 7. Shifting either endpoint away from 1 and 6 (using 0 as the lower bound, or 7 as the upper bound) always lets the call produce a value outside the 1-to-6 range that A is required to hold.