i'm currently working on a project and I seem to have encountered a logical bug. My project is in pure python and consists of two files: ui.py and read.py.
The ui handles a simple cmd ui and a cache file to save user settings.
Read gets data from the cache file via import from ui in order to parse some files ( the file parsing isn't related to the problem) Here is the code for ui:
def read_cache():
with open("cache.txt", "r+") as f:
byt = f.read()
byt = byt.split()
return byt
def write_cache(a, b):
with open("cache.txt", "w") as f:
f.write(a + "\n" + b)
def ui():
g = raw_input("Use cache file? (y/n): ")
if g == "y":
i = read_cache()
return i
elif g == "n":
zipname = raw_input("Zip file name: ")
direc = raw_input("Zip file folder directory: ")
write_cache(zipname, direc)
i = [zipname, direc]
return i
else:
print("ERROR: Please input a valid y/n \n")
ui()
And here is the code for read (only related parts where the error occurrs):
import ui
x = ui.ui()
zipname = x[0]
direc = x[1]
The error is: Traceback (most recent call last):
File "read.py", line 11, in <module>
zipname = x[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
And it seems to occur when I input an invalid input during the y\n question, and than input a valid one. AKA something goes wrong in the else clause in ui.ui()
Any ideas? I tried importing only ui() from ui, I tried running ui in ui and assigning a var there.
Thanks in advance for the help.
Edit: changing the code to return ui() seems to fix the problem but I don't understand how. Can someone explain please?
Second edit: I still don't know why return ui() works but my whole code could be better build by changing everything to a while loop with an if loop nested. Than there is no need for recursion. Thanks @dercz and @zondo