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?

4
  • Is your code properly indented? Commented Feb 15, 2016 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. Commented Feb 15, 2016 at 19:02
  • Possible duplicate of Python variable scope error Commented Feb 15, 2016 at 19:08
  • Sorry, I accidentally turned off my email notifications so I just saw these. I'll be sure to do so. Commented Feb 16, 2016 at 15:09

2 Answers 2

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.

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

Comments

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()

Comments

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.