Hi!
I know some of you had wanted to learn Python and below and attached is a
simple Python program to study and learn. It is a address book program so you
can save addresses and phone numbers for any person you have or just use it as
some kind of memo pad or calendar.
Note that Python is a indent language where an end statement is not
normally used, all is based on it's level of indentation with respect to
commands.
Also note that the ElIf statement is used as is Switch or Case statement
that other languages use.
The def statement is a procedure call and there is a function statement
also to return text, along with class statements and many other commands.
This is a nice working tutorial program to learn Python with. The # symbol
is used for comments and the triple quote can also be used or assigned to a
variable to be called as this program does for the list of options. The only
difference is you must end the triple quotes with another set of triple quotes,
"""
Enjoy playing with it, but in order to run this you must first download
Python from the Python.org web site and the most stable is either 2.5 or 2.7
version where the 2.7 version has more features for scripting in it.
Bruce
#Loading the Address Book using a global file name:
import os
file_name = "add_book.txt"
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()
print "%s File Saved and Closed!" % file_name
#Notice we need to add a newline character ('\n')
#when we write the data.
#Copying the Address Book With New Name!
def copy_Book (book):
save = 1
file_name2 = text_Input(" Enter Name Of File To Save Address Book:")
if file_name2 == "":
print "No File Saved!"
save = 0
if save == 1:
try:
store = open(file_name2, 'w')
except:
print "File Error! No File Saved!"
save = 0
if save == 1:
for name,entry in book.items():
store.write(name + '\n')
store.write(entry + '\n')
store.close()
print "%s File Saved and Closed!" % file_name2
#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-7): ") )
except:
choice=0
if 7 < choice < 1:
e=1
print "Bad Entry, Only single digit number!"
return choice
#DO TEXT INPUT WITH ENTRY ON NEXT LINE!
def text_Input(prompt):
print prompt
return (raw_input(">> "))
#Adding an Entry
def add_Entry (book):
name = text_Input("Enter a name: ")
if name != "":
entry = text_Input("Enter Street, City/Town and Phone Number: ")
if name != "" and entry != "":
book[name] = entry
else:
print "No entry saved!"
#EDIT ENTRY!
def edit_Entry (book):
name = text_Input("Enter a name: ")
if name in book:
print "%s > %s" % (name, book[name])
entry = text_Input("Enter Street, City/Town and Phone Number: ")
else:
name = ""
if name != "" and entry != "":
book[name] = entry
else:
print "No edit!"
#Removing an entry
def remove_Entry (book, del_Book):
name = text_Input("Enter a name: ")
if name in book:
print "%s Deleted!" % name
del_Book[name] = book[name]
del(book[name])
else: print "Sorry, no entry for: ", name
#RESTORING A DELETED ENTRY!
def restore_Entry (book, del_book):
if len( del_book) == 0:
print "Nothing To Restore!"
else:
for name,entry in del_book.items(): pass
print "%s At: %s" % (name, entry)
book[name] = entry
del(del_book[name])
#Finding an entry
def find_Entry (book):
name = text_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) Restore Entry
4) Find Entry
5) Edit Entry
6) Copy Book
7) Quit and save
"""
# MAKE LIST FOR DATA AND FETCH DATA ON START-UP!
the_Book = {}
book_Dels = {}
read_Book (the_Book)
# MAKE CHOICES UNTIL QUITTING!
choice = 0
while choice != 7:
choice = get_Choice (the_Menu)
if choice == 1:
add_Entry (the_Book)
elif choice == 2:
remove_Entry (the_Book, book_Dels)
elif choice == 3:
restore_Entry (the_Book, book_Dels)
elif choice == 4:
find_Entry (the_Book)
elif choice == 5:
edit_Entry (the_Book)
elif choice == 6:
copy_Book (the_Book)
elif choice != 7:
print "Invalid choice, try again"
save_Book (the_Book)
e = raw_input(" Hit Enter Key To Quit! ")
#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
---
This email is free from viruses and malware because avast! Antivirus protection
is active.
http://www.avast.com