[Tutor] Random order program

Christian Witts cwitts at compuscan.co.za
Mon Nov 28 09:57:28 CET 2011


On 2011/11/28 12:17 AM, myles broomes wrote:
> I requested help for this code earlier and I thought it had been corrected but apparently, there is still a problem with it...
>
> #random word order program
> #the user gives the program a list of words
> #the program then returns them in a random order
>
> import random
>
> #explain the purpose of the program to the user
> print("At the prompt, type in a list of words and they will be returned in a random order.")
>
> #get the users input for the list of words, one by one
> first_word = input("Please enter your first word: ")
> second_word = input("Please enter your second word: ")
> third_word = input("Please enter your third word: ")
> fourth_word = input("Please enter your fourth word: ")
> fifth_word = input("Please enter your fifth word: ")
>
> #create a tuple containing the users words of the words
> word_list = (first_word,second_word,third_word,fourth_word,fifth_word)
>
> #create an empty list that the words will go into to be returned in a random order
> random_word_list = []
>
> print("Now your list will be displayed in a random order.")
>
> #random order list
> while len(random_word_list) != len(word_list):
>          word = random.choice(word_list)
>          if word not in random_word_list:
>                  random_word_list += word
>
> #display the random word list
> print(random_word_list)
>
> input("Press enter to exit...")
>
> And once again, the following is displayed....
>
> At the prompt, type in a list of words and they will be returned in a random order.
> Please enter your first word: one
> Please enter your second word: two
> Please enter your third word: three
> Please enter your fourth word: four
> Please enter your fifth word: five
> Now your list will be displayed in a random order.
>
> Then the program just stops for some reason. Again, any help is much appreciated.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

Is there anything wrong with just doing the following ?

from random import shuffle
# just type words with space separating the items
# ie. one two three four five
words = input('Please enter a list of words: ')
word_list = words.split()
print word_list
shuffle(word_list)
print word_list

-- 

Christian Witts
Python Developer
//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111128/df66be20/attachment.html>


More information about the Tutor mailing list