Bird
Raised Fist0

Why does this code raise an error?

medium📝 Debug Q7 of Q15
Python - Standard Library Usage
Why does this code raise an error?
import random
print(random.choice(123))
ANo error, it prints a random digit
Brandom.choice cannot be used without a list
Crandom module is not imported
Drandom.choice expects a sequence, not an integer
Step-by-Step Solution
Solution:
  1. Step 1: Check argument type for random.choice

    random.choice() needs a sequence like list or string.
  2. Step 2: Identify error cause

    Passing integer 123 causes TypeError because int is not iterable.
  3. Final Answer:

    random.choice expects a sequence, not an integer -> Option D
  4. Quick Check:

    choice needs sequence input [OK]
Quick Trick: random.choice needs list or string, not int [OK]
Common Mistakes:
MISTAKES
  • Passing int instead of list
  • Assuming no error
  • Confusing import errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes