Exam-style question
Try this first
Which Python program fragment correctly generates and displays a random integer from 1 to 10 inclusive?.
- A.import random number = random.randint(1, 10) print(number)
- B.import random number = random.randint(1, 9) print(number)
- C.import random number = random.randint(0, 10) print(number)
- D.import random number = random.integer(1, 10) print(number)
Answer and explanation
Concise answer
import random number = random.randint(1, 10) print(number)
Why this is correct
The random module is imported, randint is used with the inclusive limits 1 and 10, and the generated value is displayed. The other options use an incorrect range or an incorrect function name.
Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.
Common mistake
No common mistake is linked to this question yet.
