On 11/27/2011 11:28 AM, myles broomes wrote:
Im trying to make a program where the user enters 5 words and then the list of 
words a is returned in a random order. Heres the code I have come up with so 
far:

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

When I run the program, this is what 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.


...And then nothing happens after that - no error, no nothing.

 random_word_list != len(word_list) will always be true. Therefore the loop 
will never end.

What should that expression be? See if you can figure out why it is always true 
and what to change to get the desired result.

In theory the loop could run for a long time, as you could get an arbitrarily 
long run of words that are already in the random_word_list. How could you fix 
that?

There are any number of ways to simplify and improve the program. Are you 
interested in hearing about them?

For one study the functions in the random module.


--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to