辅导CSc 110、讲解diff-testing、辅导Python编程语言、Python设计辅导

- 首页 >> Python编程

2018/11/26 CSc 110 Contacts - Benjamin Dicken

http://benjdd.com/courses/cs110/fall-2018/pas/contact_manager/index.html 1/3

Contact Manager

Almost everyone these days has some type of contact set for managing the phone numbers and email

addresses of friends, co-workers, and acquaintances. Some use Google contacts( contacts.google.com ),

others use the contacts app on their phone, and some still even prefer old-fashioned paper-and-pencil

organization.

In this assignment, you will implement a very simple contact organizer application in Python. You should write a

program called contact_manager.py . Using this application, you will be able to request contact information, as

well as add new contacts to the contact manager.

You are NOT allowed to use dictionaries or lists (with one exception). The exception is that you MAY use lists

when it comes to reading in content from a file and splitting the lines, but you may not use them for storing the

contacts for later access. Thus, you should use sets and tuples.

Specifically, I recommend storing the contacts in a set of tuples. The set will contain all of the contacts. Each

contact will be represented by a tuple, and each tuple should contain exactly three values: name, email, and

phone number. Your program should not allow a contact with the same name, email, and phone number to

appear twice. Using a set can help you maintain this property of the contact list!

Reading from a file

The contacts should also be able to be loaded from a file called contacts.txt .

contacts.txt will have a file format that looks like the following:

Bill Jones | bjon@gmail.com | 520-999-8765

Sam Dekker | dekker@apple.com | 123-456-7890

Janet Keller | kel@yahoo.com | 456-2356

James Jamie | jjamie@gmail.com | 435-234-2334

The precise names, emails, and phone numbers may vary. Also, the number of contacts in the file may vary.

You should not assume that the file has any particular number of contacts in it, but you can assume that the file

exists. You can assume that the contacts.txt file is in the same directory as the program you are implementing

( contact_manager.py ).

When contact_manager.py begins, it should read in and save all of the contact names, emails, and phone

numbers into the program. Since you won’t know how many contacts are in the file ahead-of-time, you should

use a data structure to store the contacts.

Command Types

2018/11/26 CSc 110 Contacts - Benjamin Dicken

http://benjdd.com/courses/cs110/fall-2018/pas/contact_manager/index.html 2/3

After optionally reading in all of the contacts from the contacts file, your program should repeatedly prompt the

user to enter in commands. Your program should support three main types of commands.

Showing contacts

The user can enter a command of the form show contacts with A B , where A is either name , email , or phone , and B

is the name or email or phone to search for. If the command that a user types begins with show contacts with ,

then you can assume that the A and B components exist. You should loop through the set of contacts and print

out all of the ones where the name, email, or phone matches the search string.

Contacts should be reported with the following format:

C's contact info:

email: A

phone: B

However, you should replace C, A, and B with the actual name, email, and phone number. If there are no

matching contacts, then print

None

Either way, after processing the command, the program should prompt the user for the next command

afterward.

Adding a Contact

When add contact is typed, the program will continue to prompt the user for three additional values: The name,

email, and number of the contact. Once it prompts the user for these three things, the program will add this

new contact to the set of contact info that are stored in the program. Below is an example of what this should

look like:

> add contact

name: X

email: Y

phone: Z

contact added!

This is what it would look like, but the X , Y , and Z values will be custom values entered by the user. Each time

an add contact command is processed, a new contact should be added to the set. You do not need to validate

that X , Y , and Z are any particular type of input.

Exit

If the user types exit (all lower case) then the program should print an exit message and end. For example:

> exit

Goodbye!

2018/11/26 CSc 110 Contacts - Benjamin Dicken

http://benjdd.com/courses/cs110/fall-2018/pas/contact_manager/index.html 3/3

Extra Credit: saving

Complete the following feature for extra credit. When the user requests the program to exit, the program

should save the contents of the contact set back into the contacts.txt file. This is important so that your

program will remember all of the newly-added contacts when it runs again in the future.

If contacts were added via the add contact command, these must be saved into the contacts.txt file, using the

format described earlier.

Other

If any other command is entered, your program should print Huh? and then prompt the user for another

command.

I will be providing a lot of example runs/output on the diff-testing tool on the class website. You should take

care to make sure that the output of your program very closely matches the example runs that I post. That will

be the best way to make sure you are implementing the program correctly.

Diff-Testing

Several test-cases on the diff-testing tool on the class website. The text of each test will have multiple parts.

The contents of contacts.txt should be from the get-go (before you run the program)

The program input

The program printing/output that you should match

Note that for some of the test cases, the ordering of contacts does not need to match perfectly in some cases.

This is because order is not guaranteed for elements in a set!

Submission and grading

It is due on 11/27/2018 at 7:00pm.

You should put all of your code in to a python file named contact_manager.py . Submit contact_manager.py on

Gradescope


站长地图