-4

Here's the question:

07. Define a function named import_zip_codes_json() which imports the zip_codes.json as a list in working directory.

Here's the code:

import json
with open('zip_codes.json') as list:
  zip_codes_json = json.load(list)

(The 'def' line is defined by teacher, and what I've wrote is the below part.)

def import_zip_codes_json() -> list:

    zip_codes_json = import_zip_codes_json()
    return zip_codes_json

type(zip_codes_json)

I got 'list' for my return, and it's right. But after running all the codes, I've got RecursionError.

ERROR: test_07_import_zip_codes_json (__main__.TestMidterm)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython-input-524-ba9f8f20ec9d>", line 48, in test_07_import_zip_codes_json
    zip_codes_json = import_zip_codes_json()
  File "<ipython-input-516-0c2ae51a67d5>", line 10, in import_zip_codes_json
    zip_codes_json = import_zip_codes_json()
  File "<ipython-input-516-0c2ae51a67d5>", line 10, in import_zip_codes_json
    zip_codes_json = import_zip_codes_json()
  File "<ipython-input-516-0c2ae51a67d5>", line 10, in import_zip_codes_json
    zip_codes_json = import_zip_codes_json()
  [Previous line repeated 941 more times]
RecursionError: maximum recursion depth exceeded

I've searched on Google for solutions, and I've tried to use sys.setrecursionlimit(20000)

import sys
sys.setrecursionlimit(20000)

but I still got RecursionError

ERROR: test_07_import_zip_codes_json (__main__.TestMidterm)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython-input-562-ba9f8f20ec9d>", line 48, in test_07_import_zip_codes_json
    zip_codes_json = import_zip_codes_json()
  File "<ipython-input-554-0c2ae51a67d5>", line 10, in import_zip_codes_json
    zip_codes_json = import_zip_codes_json()
  File "<ipython-input-554-0c2ae51a67d5>", line 10, in import_zip_codes_json
    zip_codes_json = import_zip_codes_json()
  File "<ipython-input-554-0c2ae51a67d5>", line 10, in import_zip_codes_json
    zip_codes_json = import_zip_codes_json()
  [Previous line repeated 19941 more times]
RecursionError: maximum recursion depth exceeded

How can I fix this?

3
  • 2
    Why does your function import_countries_json() call itself? Also what is import_zip_codes_json() that you have in your full error traceback? Commented Oct 29, 2022 at 14:26
  • You need the break condition. Commented Oct 29, 2022 at 15:46
  • 2
    I see you have updated the code in your question to match the full error traceback. So why does your function import_zip_codes_json() call itself? What does that have to do with the snippet above that with the json.load(list) in it? Why does question 07 state: "imports the zip_codes.json"? Commented Oct 29, 2022 at 15:55

1 Answer 1

0
def import_zip_codes_json() -> list:

    zip_codes_json = import_zip_codes_json()
    return zip_codes_json

The first thing import_zip_codes_json() does is call itself, which then calls itself, which then calls itself.... forever. Or until the stack blows up and you get the error.

Why are you doing this?

Sign up to request clarification or add additional context in comments.

2 Comments

@GinoMempin the answer does end with a question, but the text below the code definitely identifies the issue that the OP is asking about.
@MichaelDelgado The question was "How can I fix this?" While it identifies why the recursion error is happening, can't provide a fix without understanding why the OP defined the function like this.... which should be a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.