我正在处理我的代码。我基本上希望我的代码循环遍历字典,看看这个raw_input是否与字典中的键相同。它不会循环通过字典,我不知道为什么。这是我的循环代码:
for filename in files:
if filename.lower() == deletename.lower():
break
del files[filename]
print "\nFile Removed"
print "--------------------------------------------"
startup()
然后下面是我的完整代码:
import datetime
import time
files = {}
# g = open('files.txt', 'r')
# g.read(str(files))
# g.close()
def startup():
print "\n ------------------- "
print " FILE SYSTEM MANAGER "
print " ------------------- "
print "\n What would you like to do with your files?"
print " To make a new file type in: NEW"
print " To edit a current file type in: EDIT"
print " To delete a current file type in: DELETE"
print " To view all current files type in: ALL"
print " To search a specific file type in: SEARCH"
chooser = raw_input("\n Please enter NEW, EDIT, DELETE, ALL, or SEARCH: ")
if chooser.lower() == "new":
newfile()
elif chooser.lower() == "edit":
editfiles()
elif chooser.lower() == "delete":
deletefiles()
elif chooser.lower() == "all":
allfiles()
elif chooser.lower() == "search":
searchfiles()
else:
startup()
def newfile():
filename = ""
filetext = ""
while filename == "":
print "--------------------------------------------"
filename = raw_input("\n Please input your new files name: ")
while filetext == "":
filetext = raw_input("\n Please input the text for your new file: ")
filedate = datetime.date.today()
files[filename] = {filedate:filetext}
# f = open ('files.txt', 'w')
# f.write(str(files))
# f.close()
print "\n File Added"
print "\n--------------------------------------------"
startup()
def editfiles():
pass
def deletefiles():
print "--------------------------------------------"
print " To delete a file type in: DELETE"
print " To view all current files type in: ALLFILES"
print " To cancel type in: CANCEL"
wheretogo = raw_input("\n Please enter DELETE, ALLFILES, or CANCEL: ")
if wheretogo.lower() == "delete":
print "\n To delete a file type in its name"
print " To cancel type in: CANCEL"
deletename = raw_input("\n Please type in the file's name or CANCEL: ")
if deletename.lower() == "cancel":
startup()
else:
for filename in files:
if filename.lower() == deletename.lower():
break
del files[filename]
print "\nFile Removed"
print "--------------------------------------------"
startup()
else:
pass
print "\nFile not found!"
deletefiles()
elif wheretogo.lower() == "allfiles":
print "\n--------------------------------------------"
for filename in files:
print "File Name: " + str(filename)
print "--------------------------------------------"
print "\n To delete a file type in: DELETE"
print " To cancel type in: CANCEL"
wheretogo = raw_input("\n Please enter DELETE or CANCEL: ")
if wheretogo.lower() == "delete":
pass
elif wheretogo.lower() == "cancel":
startup()
else:
startup()
elif wheretogo.lower() == "cancel":
startup()
else:
startup()
def allfiles():
filetexttotal = ""
for filename in files:
print "\n--------------------------------------------"
print "File Name: " + str(filename)
for filedate in files[filename]:
print "File Date: " + str(filedate)
for filetext in files[filename][filedate]:
filetexttotal = filetexttotal + str(filetext)
print "File Contents: " + str(filetexttotal)
filetexttotal = ""
print "--------------------------------------------"
startup()
def searchfiles():
pass
startup()
答案 0 :(得分:2)
网络在这里找到了正确的答案:在if之后你有一个break
。在那之后你永远不会打任何代码。
答案 1 :(得分:1)
Break会突破循环,继续移动到下一个项目。
编辑:考虑评论。
for filename in files:
if filename.lower() == deletename.lower():
continue
del files[filename]
print "\nFile Removed"
print "--------------------------------------------"
startup()