[Tutor] Program

Corey Richardson kb1pkl at aim.com
Wed May 18 14:39:47 CEST 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 05/18/2011 08:26 AM, Johnson Tran wrote:
> Hi Again All,
> 
> I had a couple questions about my program:
> 
> def CollectNames():
>     
>     answer_set=set([])
>     sorted_list = sorted(answer_set)
>     word=raw_input("Name #1: ")
> 
>     word=raw_input("Name #2: ")
>     
>     word=raw_input("Name #3: ")
> 
>     word=raw_input("Name #4: ")
> 
>     word=raw_input("Name #5: ") 
>     
>     print "Your answer's sorted: ", ','.join(sorted_list)
> 
> CollectNames()
> 
> 
> 1.) how do i add each answer given to the list so it is printed at the end?

Well, you can't have ALL the answers printed at the end, but one way is
to use a list and .append(word) each time.

> 2.) also im trying to modify the program so if the user puts in the same name, it will give an make them try again until they have 5 completely different names.

Now, you might see a pattern in your prompt. Each time you ask for
input, you increment the name number. Perhaps this is the prime place
for a loop? If you add in a loop, it will also be fairly easy to add in
another loop to make sure they enter a name not in the list. So, your
pseudo-code might look something like this:

for i in range(6):
    make prompt string;
    get name;
    while name in names_gotten:
        get name;
    add name to names_gotten;
print names_gotten;

(P.S., PEP 8 says functions should be lowercase_with_underscore,
not CamelCase)
- -- 
Corey Richardson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJN074TAAoJEAFAbo/KNFvpdHwIAK1Ji+4Z3Fac0wtH2EgBDwp2
K8t10KpbtRYfWOCjiYfBAzZFWLrQ9I+lrmdth7Asf0ANg72U4gPHkp82ZbO8mhyz
02eDBPXboAmLcntxsxcmMkNlG1xPVeXjcriGwX/VcN2AguGKvrKkKbkkT+Ar+bWZ
ZpjH0ycNsAUTNeQLQEHJQJtPMktJ13XvlrjHN0YVoLpk812rAn+nuTZq+p0J5fzc
hCgyxUiRcHYllXZv/1AegOWbfon3BMur9fpV2UMo8JcsRTHto3Lb5c3jHqApNjfc
M48rpigGXjOzowj0WbsMmSHrskBglcSAy+xo/Ti0vnBXDMU3secWFWkaxDtdidk=
=oCrn
-----END PGP SIGNATURE-----


More information about the Tutor mailing list