Bird
Raised Fist0

The following code throws an error. What is the mistake?

medium📝 Debug Q14 of Q15
Python - Structured Data Files
The following code throws an error. What is the mistake?
import json
data = {'x': 1, 'y': 2}
print(json.dumps(data, indent=2.0))
Ajson.dumps requires a second argument for separators
BThe data dictionary keys must be strings only
Cjson.dumps cannot format dictionaries
DThe indent parameter should be an integer, not a float
Step-by-Step Solution
Solution:
  1. Step 1: Identify the error cause

    The indent parameter is given as a float 2.0 instead of an integer 2.
  2. Step 2: Understand parameter type requirements

    json.dumps expects indent to be an integer number of spaces for formatting, passing a float causes a TypeError.
  3. Final Answer:

    The indent parameter should be an integer, not a float -> Option D
  4. Quick Check:

    Indent must be int, not float [OK]
Quick Trick: Check indent type: must be integer, not float [OK]
Common Mistakes:
MISTAKES
  • Passing indent as float instead of int
  • Thinking keys must be strings for json.dumps
  • Expecting separators argument is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes