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)
Model answer
What a good answer should say
- import random number = random.randint(1, 10) print(number)
Explanation
Why this works
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.
Common mistake
No common mistake is linked to this question yet.
