Python,条件问题

时间:2016-02-15 18:58:31

标签: python conditional

我目前正在python中编写一个数据库,有转换文件,而且它们都运行得很好。登录完成后我的代码如下:

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

我用*标记了我的名字,所以你可以忽略这些。这个文件的大部分工作正常,但如果你看到while循环,那就忽略了等待结束命令的elif。无论我给它多少次结束命令,它都会忽略并继续。帮助

2 个答案:

答案 0 :(得分:2)

您的功能不return。我冒昧地重新编写了你的​​主要部分:

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

现在它实际上return一个值(True)完成后,end将相应地设置。 请注意,end函数中的choosefile()变量不是与您在其外部定义的变量相同。

答案 1 :(得分:0)

感谢所有帮助过我的人。以下是工作代码,返回语句就像一个魅力。非常感谢。

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