[Tutor] Program
Johnson Tran
aznjonn at me.com
Thu May 19 20:23:39 CEST 2011
So I figured out how to use the loop method, thanks. I still cannot seem to figure out how to use Len() to show the output of my answers (from all my googling Len() seems to be used to count characters?) Also, I am not really sure I understand how to use the append method of the list.
Here's my best guess so far:
def CollectNames():
for attempt in range(1,6):
word=raw_input("Name #%d" % attempt)
list.append("new")
print "All the names in alphabetical order are ", len(L);
And just to recap, I was trying to get all the names outputted after the last name was collected (and sort them in alphabetical order. Took my sort commands out until I could figure out how to get any output first....
Thanks for any help.
On May 18, 2011, at 7:57 AM, Alan Gauld wrote:
>
> "Johnson Tran" <aznjonn at me.com> wrote
>
>> I had a couple questions about my program:
>>
>> def CollectNames():
>> answer_set=set([])
>> sorted_list = sorted(answer_set)
>
> This creates an empty set then sorts it and stores
> the result as an empty list. I'm not sure what you
> think it does but I'm guessing that's not it...
>
>> 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: ")
>
> Do you know about loops yet?
> Any time you find yourself repeating code like
> this think about a loop. A for loop could be
> used here:
>
> for attempt in range(1,6):
> word = raw_input("Name #%d" % attempt)
>
> Although storing all the names in the same variable
> is also probably not what you want. You need to
> add word to your list using the list append() method.
>
>> print "Your answers sorted: ", ','.join(sorted_list)
>
> And this is where you probably want to call sorted()...
>
>> 1.) how do i add each answer given to the list so it is printed at the end?
>
> Use the append method of the list
>
>> 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.
>
> A combination of a while loop and a set and the len() function
> might work here. Keep adding to the set while the length of the
> set is <5.
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list