<br><br><div class="gmail_quote">On Thu, Sep 9, 2010 at 5:42 PM, Joel Goldstick <span dir="ltr">&lt;<a href="mailto:joel.goldstick@gmail.com">joel.goldstick@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5"><br><br><div class="gmail_quote">On Thu, Sep 9, 2010 at 4:51 PM, lists <span dir="ltr">&lt;<a href="mailto:lists@justuber.com" target="_blank">lists@justuber.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Hi tutors,<br>
<br>
Still on my Python learning journey! I&#39;ve just competed an exercise<br>
which asks the student to &quot;Create a program that creates a list of<br>
words in random order. This program should print all the words and not<br>
repeat any.&quot; I&#39;ve printed the list for my own needs. The list<br>
randwords aims to answer the specific request of the exercise author.<br>
<br>
If anyone has the inclination and a minute to spare, please run your<br>
eyes over my answer. It works, but is it an OK way to approach the<br>
exercise?<br>
<br>
Thanks again :-D<br>
<br>
Chris<br>
<br>
import random<br>
<br>
#LIST<br>
words = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;,<br>
&quot;eight&quot;, &quot;nine&quot;, &quot;ten&quot;, &quot;eleven&quot;]<br>
randwords = []<br>
<br>
while words: #has entries in it<br>
    wordslen = len(words) #get the length of the list<br>
    index = random.randint(0, wordslen -1) #get a random index<br>
    chosenword = words[index]<br>
    randwords.append(chosenword) #append the random word to a new list<br>
    del words[index] #del the word from the old list<br>
<br>
for word in randwords:<br>
    print word # print them<br clear="all"></blockquote></div><br></div></div>Several small and not so small points:<br><br>1. you assign wordslen each pass through your loop.  While it doesn&#39;t matter in a small loop, it wastes time on the order of the size of your list.  Instead move wordslen = len(...  above your while loop.  Any time you put code in a loop that doesn&#39;t change in each iteration, you should move it out of the loop.<br>

<br>2. since you only use chosenword once in your loop, you could remove chosenword = words[index] line and replace chosenword in the append(... with words[index]<br><br>3. your list doesn&#39;t contain any duplicate words.  Since  your program is supposed to catch this, you should add a duplicate to see if it works.  (No!) <br>

<br>4. I think your line del words[index] is supposed to help out with item 3 but it doesn&#39;t.  It just removes the word you just used selected.<br><br>5. And finally, I think you want to print  <br><br>Just minor points.  nice job -- getting there<br>

-- <br><font color="#888888">Joel Goldstick<br><br>
</font></blockquote></div><br><br>I looked into this a little more, and although I&#39;m ok with my comments, I did some experimenting and looked into the stuff in random.  I came up with this.  It may not be helpful since you may be learning about loops and randint, but this works:<br>
<br>import random<br><br>#LIST<br>words = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;,<br>                    &quot;eight&quot;, &quot;four&quot;, &quot;nine&quot;, &quot;ten&quot;, &quot;eleven&quot;]<br>
<br>word_set = list(set(words))    # this removes duplicates since set contains one of each value.  Then convert back to a list<br>print &quot;original words: &quot;, words    # just wanted to see my original list<br>print &quot;removed duplicates with set:&quot;, word_set   # this shows that I don&#39;t have duplicates<br>
random.shuffle(word_set)  # this shuffles the list in place.  Now word_set is shuffled (randomized!)<br>print &quot;randomized: &quot;, word_set   # see!<br><br><br clear="all"><br>-- <br>Joel Goldstick<br><br>