[Tutor] Re: Re: problem writing random_word function (original) (raw)

Matt Smith smith-matt at tiscali.co.uk
Thu Jul 22 23:19:13 CEST 2004


Thanks for all of the help, I've modified the function to hopefully take into account all of the suggestions. I know the filename is still hard coded. I'm leaving it as so until I decide how to get the file name to use (maybe an option in the program or a command line argument). Anyway, heres the function:

import random def random_word(word_length): """Returns a random word of length word_length""" file = open("hangman_words.txt","r") word_list = [] for word in file: word = word.strip() if len(word) == word_length: word_list.append(word) file.close() if len(word_list) == 0: return 0 else: return random.choice(word_list)

I'll post the full program for people to have a look at when I've cleaned it up a bit. Cheers, Matt.



More information about the Tutor mailing list