On 28/10/2012 21:37, Sandra Beleza wrote:
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!


In your original getNames do something like this.
Initialise a counter to zero.
Every time you get a valid name increment the count.
If the count is three you're finished.

--
Cheers.

Mark Lawrence.

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to