[Tutor] Random order program

Dave Angel d at davea.name
Mon Nov 28 00:25:27 CET 2011


On 11/27/2011 05:17 PM, 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...
>
To start with, tell us what version of Python, and what operating system 
platform you're using.  I'm assuming Python 3.x and Linux.   In this 
case the first matters, the second probably does not.

> #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
>
If you use  += operator with list on the left side, it assumes something 
compatible with list on the right.  So either use
              random_word_list  +=  [word]
Or else use random_word_list.append(word)

As it's currently coded, it take each character of the word and adds 
them to the list, so the list doesn't match the original at all.

Next problem you might encounter is if the user doesn't give unique 
words.  If they repeat one of them, the loop will never terminate.  You 
figure  out why.

> #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.
>

As others have pointed out, there is at least one function in random 
that's a better match to  your problem.  You could collapse the loop to 
one line, and duplicates wouldn't be an issue.

Another approach would be to make a set out of the original input.  
Duplicates will be thrown out (you might want to tell the user, 
though).  Then you take the set as an argument to the function I just 
hinted at, and out comes your answer.


-- 

DaveA



More information about the Tutor mailing list