[Tutor] Need to create code

Alan Gauld alan.gauld at btinternet.com
Wed Dec 11 01:13:05 CET 2013


On 10/12/13 15:45, Matthew Thomas wrote:
> Write a function named *SSN2Name* with an interactive loop.

This is obviously some kind of homework exercise. We do
not do your homework for you but we will give you pointers
or clarify issues if you get stuck.

But we expect you to make a start, post your code and
any questions. If you have any errors post the entire error
message, do not summarize.

If you are stuck tell us what happened, what you expected,
what version of python, what OS you use. The more specific
the question and information you give us the better we can
answer.

> takes the dictionary named*data*as input argument where this dictionary
> stores the key, value pairs of SSN, name of person.

Do you know how to write a function?
Do you know how to create/use a dictionary?

> string format 'xxx-xx-xxxx' and name is also a string. Each iteration of
> the functions's loop should prompt the user to enter an SSN in the
> required format and next, it should print the name of the person if
> he/she is found in the dictionary, otherwise a message like "person not
> found" if that SSN does not exist in the keys of the dictionary.

Do you know how to write a loop?
How to get input from a user? How to access a dictionary?
How to print output?


> loop should stop iterating when the user inputs the empty string "" when
> prompted for SSN.

Do you know how to test for an empty string?
And how to exit a loop when you find one?

> Write the full function definition in your answer. The function outline
> is given below with comments provided as code hints -

> def SSN2Name(data):
>       # create a loop that repeats forever (while True loop)
>              # within the loop prompt user for SSN
>              # if user input is empty string break out of loop
>              # if user input is an SSN found as a key in the dictionary,
> print the value corresponding to that key
> # if user input is not found as a key in the dictionary print message
> saying person was not found

What part of that description, if any, do you have a problem with?

If you don't have a specific question yet then just go ahead and
try to create the code. Come back when you have a specific question
and we can try to help.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list