[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Help with the 3rd Lecture, first tutorial



Kevin,
 
    Here is another example of the os.path used and defaults to the path the program resides upon loading. The path is where you reside when loading Python25. Python25 will load within the environ path that windows has been told to look in. The address book will also be saved in the directory in which you started it. All values are defaults.
    You and anyone else which wants a simple address book here is a good simple program. Not all error checking is there but OK for a starter.

#Loading the Address Book using a global file name:
import os
file_name = "add_book.dat"

def read_Book (book):
    if os.path.exists(file_name):
        store = open(file_name,'r')
        for line in store:
            name = line.rstrip()
            entry = store.next().rstrip()
            book[name] = entry
        store.close()
 
#Notice the use of rstrip() to remove the new-line character from the end of the line.
#Also notice the next() operation to fetch the next line from the file within the loop.
 
#Finally #notice that we defined the filename as a module level variable so we
#can use it both in #loading and saving the data.
 
#Saving the Address Book
def save_Book (book):
    store = open(file_name, 'w')
    for name,entry in book.items():
        store.write(name + '\n')
        store.write(entry + '\n')
    store.close()
 
#Notice we need to add a newline character ('\n')
#when we write the data.
 
#Getting User Input
def get_Choice (menu):
    print menu
    e=1
    while e==1:
        e=0
        try:
            choice = int( raw_input("Select a choice(1-4): ") )
        except:
            print "Bad Entry, Only single digit number!"
            e=1
    return choice
 
#Adding an Entry
def add_Entry (book):
    name = raw_input("Enter a name: ")
    entry = raw_input("Enter street, town and phone number: ")
    book[name] = entry
 
#Removing an entry
def remove_Entry (book):
    name = raw_input("Enter a name: ")
    if name in book:
        del(book[name])
    else: print "Sorry, no entry for: ", name
 
#Finding an entry
def find_Entry (book):
    name = raw_input("Enter a name: ")
    if name in book:
        print "%s > %s" % (name, book[name])
    else: print "Sorry, no entry for: ", name
 
#Quitting the program
#Actually I won't write a separate function for this, instead
#I'll make the quit option the test in my menu while loop.
#So the main program will look like this:
 
def main():
    the_Menu = """
1) Add Entry
2) Remove Entry
3) Find Entry
4) Quit and save
"""
 
    the_Book = {}
    read_Book (the_Book)
    choice = 0
    while choice != 4:
        choice = get_Choice (the_Menu)
        if choice == 1:
            add_Entry (the_Book)
        elif choice == 2:
            remove_Entry (the_Book)
        elif choice == 3:
            find_Entry (the_Book)
        elif choice != 4:
            print "Invalid choice, try again"
    save_Book (the_Book)
    print "%s Book Saved!" % file_name
 
#Now the only thing left to do is call the main() function when the program is run,
#and to do that we use a bit of Python magic like this:
 
if __name__ == "__main__":
    main()
 
#This mysterious bit of code allows us to use any python file as a module by
#importing it, or as a program by running it. The difference
#is that when the program is imported,
#the internal variable __name__ is set to the module name but
# when the file is run, the value of __name__ is set to "__main__".
#Sneaky, eh?
#Now if you type all that code into a new text file
#and save it as addressbook.py,
#you should be able to run it from an OS prompt by typing:
#C:\PROJECTS> python addressbook.py
#Or just double click the file in Windows Explorer and it should start
#up in its own DOS window, and the window will close when you
#select the quit option. Or in Linux: $ python addressbook.py