Bird
0
0

The code below causes an error. What is the fix?

medium📝 Debug Q14 of 15
Python - Variables and Dynamic Typing
The code below causes an error. What is the fix?
num = 'abc'
converted = int(num)
print(converted)
AUse float() instead of int()
BAdd quotes around int
CRemove the int() conversion
DChange 'abc' to a string of digits like '123'
Step-by-Step Solution
Solution:
  1. Step 1: Identify cause of error

    Trying to convert 'abc' to int causes a ValueError because 'abc' is not numeric.
  2. Step 2: Fix by using numeric string

    Replacing 'abc' with a string of digits like '123' allows int() to convert successfully.
  3. Final Answer:

    Change 'abc' to a string of digits like '123' -> Option D
  4. Quick Check:

    int('123') works, int('abc') errors [OK]
Quick Trick: int() needs numeric strings, not letters [OK]
Common Mistakes:
MISTAKES
  • Using float() on non-numeric string
  • Removing conversion but expecting int
  • Adding quotes around function name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes