[Tutor] help with homework

Sandra Beleza sbeleza at gmail.com
Sun Oct 28 22:37:45 CET 2012


Hi,
I have to write a script that asks the user for names one at a time, and
accept the name only if the user did not gave it before. The script has to
do this until it gets 3 unique names.

So far I have this:

def GetNames():
    names=[]
    while len(names)<3:
        name=raw_input("Name: ")
        if name in names:
            print name, "is already in the data. Try again."
        if name not in names:
            names.append(name)
    names.sort()

    for each in names:
        print "Hurray for", each +"!"
    print

However I cannot use the len() built in function. The Teacher is asking for
another solution that does not use the len() t
I know I can do it using the command:

def Ask

def GetNames(how_many):
    names=[]
    for el in range(how_many):
        name=raw_input("Name: ")
    if name in names:
        print name, "is already in the data. Try again."
    if name not in names:
        names.append(name)
    names.sort()
    for each in names:
        print "Hurray for", each +"!",
    print


I cannot get 3 names, and it is easy to understand why (because the loop
only iterates 3 times). But I don't know how to ask the user for names one
at a time and to obtain 3 names and an output that looks like this:
Name #1: Lewis
Name #2: John
Name #3: John
John is already in the data. Try again.
Name #3:
Name #3: Chris
Hurray for Chris! Hurray for John! Hurray for Lewis!


Many Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121028/4c9e0d31/attachment.html>


More information about the Tutor mailing list