2

I'm currently writing a database in python, with transitioning files, and they have all been working pretty well. My code after the login is completed is as follows:

PythonDatabase = list("/Users/*****/Desktop/PythonDatabase")



import os
flist = os.listdir(os.getcwd())
for name in PythonDatabase:
    PythonDatabase[PythonDatabase.index(name)]=name[:-3]
out = open('flist.txt','w')
for name in PythonDatabase:
    out.write(name+"\n")
out.close()



#------------------------------------------------------------------------------#
end = False
flist = open('flist.txt','r')

print("SECURITY PASSED")
print("ENTERING DATABASE")

def choosefile():
    fcho = input("ENTER FILE CHOICE, VIEW FILE LIST, OR END: ")

    if (fcho == "view file list" or fcho == "VIEW FILE LIST"):
        print (flist.read())

    elif (fcho == "END"):
        end = True


while (end == False):
    choosefile()

I blanked out my name with the *'s so you can ignore those. Most of this file is working fine, but if you see the while loop, that ignores the elif waiting for an end command. No matter how many times I give it the end command, it ignores, and continues. Help?

Idos
  • 15,053
  • 14
  • 60
  • 75
Brin Brody
  • 41
  • 8
  • Is your code properly indented? – Idos Feb 15 '16 at 19:00
  • instead of `end=True` in your function, type `return True`. Also in the `while` body, instead of simply `choosefile()`, type `end=choosefile()`. Basically the function is not returning anything to the caller. – AlessioX Feb 15 '16 at 19:02
  • Possible duplicate of [Python variable scope error](http://stackoverflow.com/questions/370357/python-variable-scope-error) – DJMcMayhem Feb 15 '16 at 19:08
  • Sorry, I accidentally turned off my email notifications so I just saw these. I'll be sure to do so. – Brin Brody Feb 16 '16 at 15:09

2 Answers2

2

Your function does not return anything. I took the liberty and re-wrote your main part of the program:

end = False
flist = open('flist.txt','r')

print("SECURITY PASSED")
print("ENTERING DATABASE")

def choosefile():
    fcho = input("ENTER FILE CHOICE, VIEW FILE LIST, OR END: ")

    if (fcho == "view file list" or fcho == "VIEW FILE LIST"):
        print (flist.read())

    elif (fcho == "END"):
        return True


while (end == False):
    end = choosefile()

Now it actually returns a value (True) when it finishes, and end will be set accordingly. Keep in mind that the end variable you had inside your choosefile() function is not the same one as you defined outside it.

Idos
  • 15,053
  • 14
  • 60
  • 75
0

Thanks to everyone who helped me out with this. The following is working code, the return statement worked like a charm. Thanks so much.

end = False
flist = open('flist.txt','r')

print("SECURITY PASSED")
print("ENTERING DATABASE")

def choosefile():
    fcho = input("ENTER FILE CHOICE, VIEW FILE LIST, OR END: ")

    if (fcho == "view file list" or fcho == "VIEW FILE LIST"):
        print (flist.read())

    elif (fcho == "END"):
        return True


while (end == False):
    end = choosefile()
Brin Brody
  • 41
  • 8