-1

I am currently working on my code. I basically want my code to loop through a dictionary and see if this raw_input is the same as a key in the dictionary. It does not loop through the dictionary though, and I do not know why. Here is my looping code:

        for filename in files:
            if filename.lower() == deletename.lower():
                break
                del files[filename]
                print "\nFile Removed"
                print "--------------------------------------------"
                startup()

Then below is my full code:

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()
Michael Jones
  • 35
  • 1
  • 1
  • 6
  • 2
    possible duplicate of [Remove items from a list while iterating in Python](http://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating-in-python) – Cory Kramer Aug 21 '14 at 23:37
  • 3
    wait a minute.... you have a `break` in your loop, it will never hit the `delete`, it will `break` and leave the loop. – Cory Kramer Aug 21 '14 at 23:38
  • 1
    Everyone pointing out the issue with `break` is right, but you can't remove items from a dictionary while you iterate over it. So once you fix the `break` issue, the code will fail because of that. See the possible duplicate Cyber mentioned to deal with that issue. – dano Aug 21 '14 at 23:46

2 Answers2

2

Cyber hit on the right answer here: You have a break right after the if. You'll never hit any code after that.

John T
  • 61
  • 2
1

Break will break out of the loop, continue will move to the next item.

edit: taking into account comments.

    for filename in files:
        if filename.lower() == deletename.lower():
            continue

        del files[filename]
        print "\nFile Removed"
        print "--------------------------------------------"
        startup()
Mike
  • 61
  • 3
  • If you're going to do something like this (`if x: continue`), then an `else` is redundant. The whole point of this style is to boost readability by avoiding unnecessary indentation. – sapi Aug 22 '14 at 01:14