diff --git a/Ch 06 Loops/EG6-10 Name printer.py b/Ch 06 Loops/EG6-10 Name printer.py index c2670de..201cf48 100644 --- a/Ch 06 Loops/EG6-10 Name printer.py +++ b/Ch 06 Loops/EG6-10 Name printer.py @@ -1,4 +1,4 @@ # EG6-10 Name printer -names=('Rob','Mary','David','Jenny','Chris','Imogen') -for •name in •names : +names= input("write your name") +for word in •names : print(name) diff --git a/Ch 09 Using classes to store data/EG9-02 Tiny Contacts Three Lists.py b/Ch 09 Using classes to store data/EG9-02 Tiny Contacts Three Lists.py index 94a50e5..0e033d1 100644 --- a/Ch 09 Using classes to store data/EG9-02 Tiny Contacts Three Lists.py +++ b/Ch 09 Using classes to store data/EG9-02 Tiny Contacts Three Lists.py @@ -1,67 +1,86 @@ -# EG9-02 Tiny Contacts Three Lists - -from BTCInput import * - -# Create the lists to store contact information -names=[] -addresses=[] -telephones=[] - -def new_contact(): - ''' - Reads in a new contact and stores it - ''' - print('Create new contact') - names.append(read_text('Enter the contact name: ')) - addresses.append(read_text('Enter the contact address: ')) - telephones.append(read_text('Enter the contact phone: ')) - -def find_contact(): - ''' - Reads in a name to search for and then displays - the content information for that name or a - message indicating that the name was not found - ''' - print('Find contact') - search_name = read_text('Enter the contact name: ') - # remove any whitespace from around the search name - search_name = search_name.strip() - # convert the search name to lower case - search_name = search_name.lower() - # Counter for the name position - name_position=0 - for name in names: - # remove any ny whitespace from around the name - name=name.strip() - # convert the name to lower case - name = name.lower() - # see if the names match - if name==search_name: - # if the names match, end the loop +import contact +import addressBook +import re +from sys import exit +import random + + +__author__ = 'Muhammad Arslan ' + +app = addressBook.addressBook(str(raw_input("Enter name of book (Will be created if doesn't exist) \n> "))) +main_menu = '\n1. Show all contacts.\n2. Add contact.\n3. Search.\n4. Delete a contact.\n5.Update contact.\n6. Exit\n\n>' + +def exitProg(): + exitMessages = ['You have my permission to die.'] + print random.choice(exitMessages) + exit(0) + +def getOption(prompt): + inp = raw_input(prompt) + try: + inp = int(inp) + except ValueError: + print 'You should have selected a proper option.' + return 13 + return inp + + +def showContacts(): + print 'show all' + +def addContact(): + flag = 13 + while flag == 13: + exp = map(lambda x: re.compile(x), [r'^([a-zA-Z]+)$', r'^(\+)?(\d)+$', r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"]) + + fName = str(raw_input('Enter first name : ')).strip() + while not exp[0].match(fName): + fName = str(raw_input('\nWrong Input\nEnter (proper) first name : ')).strip() + + lName = str(raw_input('Enter last name : ')).strip() + while not exp[0].match(lName): + lName = str(raw_input('\nWrong Input\nEnter (proper) last name : ')).strip() + + pNum = str(raw_input('Enter phone number : ')).strip() + while not exp[1].match(pNum): + pNum = str(raw_input('\nWrong Input\nEnter (proper) number : ')).strip() + + email = str(raw_input('Enter email(Blank for none) : ')).strip() + while not exp[2].match(email): + if not email: + break + email = str(raw_input('\nWrong Input\nEnter (proper) email : ')).strip() + + print app.addEntry(contact.Contact(fName, lName, pNum, email)) + + while (flag < 1) or (flag > 3): + flag = getOption('\n1. Add another.\n2. Go to main menu\n3. Exit.\n\n> ') + if flag == 2: break - # move the position down to the next name - name_position=name_position+1 + elif flag == 3: + exitProg() + else: + flag = 13 + +def searchContact(): + print 'search' + +def removeContact(): + name = str(raw_input('Enter first name of the contact: ')) + print app.removeEntry(name) + +def updateContact(): + name = str(raw_input('Enter the first name of the contact: ')) + msg, cont = app.searchEntry(name) + print msg - if name_position < len(names): - # Found a name - print('Name: ',names[name_position]) - print('Address: ',addresses[name_position]) - print('Telephone: ',telephones[name_position]) - else: - print('This name was not found.') -menu='''Tiny Contacts -1. New Contact -2. Find Contact -3. Exit program +funcs = [showContacts, addContact, searchContact, removeContact, updateContact, exitProg] -Enter your command: ''' while True: - command=read_int_ranged(prompt=menu,min_value=1,max_value=3) - if command==1: - new_contact() - elif command==2: - find_contact() - elif command==3: - break + inp = getOption(main_menu) + while inp < 1 or inp > 6: + print 'Input a proper number, moron.' + inp = getOption(main_menu) + funcs[inp - 1]() diff --git a/Ch 12 Python applications/EG12-04 Lamdba Example.py b/Ch 12 Python applications/EG12-04 Lamdba Example.py index daf41dc..704bde9 100644 --- a/Ch 12 Python applications/EG12-04 Lamdba Example.py +++ b/Ch 12 Python applications/EG12-04 Lamdba Example.py @@ -1,20 +1,6 @@ -# EG12-04 Lamdba Example +# HERE IS A EASY AND BASIC LAMBDA FUNCTION -# Create a list of numbers -numbers = [1,2,3,4,5,6,7,8] +adder = lambda x, y: x + y +print (adder (1, 2)) -# Create an increment function -def increment(x): - return x+1 - -# Use the increment function to make an incremented list -new_numbers_increment = map(increment, numbers) - -# print the incremented list -print('Increment list: ', list(new_numbers_increment)) - -# Use a lambda function to make an incremented list -new_numbers_lambda = map(lambda x : x+1, numbers) - -# print the lambada incremented list -print('Increment list: ', list(new_numbers_lambda)) +# output is "3"