What issues are present in the following Python code?
1Times asked
May 2026Last seen
May 2026First seen
💡 Model Answer
The code contains several problems:
- Operator mapping is incorrect: the subtraction operator is represented by '.' instead of '-'. This will raise a KeyError or produce wrong results.
- The print statement uses the string "12+4=" but calls calc(12,4,'*'), which multiplies instead of adding. The label does not match the operation.
- The default argument for add_to_inventory is a mutable list. Each call shares the same list, so items accumulate across calls. This is a classic Python pitfall. The function should use None as the default and create a new list inside the function.
- The print statements for add_to_inventory use a leading space before the string, which may be unintended.
- The code lacks error handling for division by zero; it raises ZeroDivisionError but does not provide a message.
Fixes:
- Use '-' for subtraction.
- Align the print label with the operation.
- Change the default argument to None and initialize a new list inside the function.
- Optionally add a message for ZeroDivisionError.
Complexity: The functions run in O(1) time; add_to_inventory is O(1) per call, but the list grows with each call.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500