[Tutor] help with homework

Alan Gauld alan.gauld at btinternet.com
Mon Oct 29 00:48:42 CET 2012

On 28/10/12 21:37, Sandra Beleza wrote:

> 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()

You should probably stop your function here by returning names.

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

It's good practice to keep the presentation logic outside
the function. Your loop would then change to

for each in getNames():

> However I cannot use the len() built in function. The Teacher is asking
> for another solution that does not use the len() t

That's bizarre. It forces you to write a bad solution.
However, Mark has already given you the necessary hint.
But using len() is absolutely the sane way to do this
(unless you are paranoid about micro performance).

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list