April 13th, 2005, 08:57 PM
generate random word
i was thinking about using a list for this but i don't think it will work. alright this is what im looking at right now. i need a program that when you run it, it randomly picks a word and saves it as a list in the variable "word". then in the variable "blanks", a list is saved with the exact same legnth as "word" but "_" in replace of the letters.
this is what i have in mind... by the way i don't know the actual programming to go along with it. i don;t know how to use random.
alright i highly doubt this will work. i can't test it because i don't know how to generate random numbers. please tell me there is a better way to do this.
blanks = 
word_list[['d','o','g'],['c','a','t'],['t','r','e','e']] #a list of the list of words i will use
random_num = generate random number between 1 and 3 #don't know actual code for this
word = word_list[random_num]
len_word = len(word)
x = 0
while x < len_word:
x = x + 1
April 13th, 2005, 09:07 PM
I've got to get going so I'l make this brief.
list = ['a', 'b', 'c,'...
This will print a random item from the list specified, in this case it is list. I'm sure you can figured out the rest, as I said already I have to get going so I don't have time for a long answer.
April 13th, 2005, 09:10 PM
it will work. It would probably just be easyer to use a list of stings of the words instead of a 2d list. Because len("blah") will print out 4 because that is how meny chariters are in the string.
for your random number. You could have search google with "python random" and got 1,420,000 results. But since you asked before you looked in the most obviouse place for the anwer, i will give you a lil example of how it works for your case
theres an extremly simple example. Remember this is by no means suitible for cryptography purposes, so please do not use it as such.
r = random.randint(1,3)
but a great info page about the module random can be found on pythons web site, http://docs.python.org/lib/module-random.html
April 13th, 2005, 09:53 PM
1/ When in doubt, read the helpfiles that come with Python - You should find the tutorial useful. You can also use the help function at the prompt.
Originally Posted by lw22
2/ You need to assign a value to the variable word_list. Your code tries to index word_list (which doesn't exist at that point in time).
3/ store your words as strings not as lists. You can convert them to lists later if you really need to (you probably don't, since strings are iterable, etc)
4/ import the random module and - assuming that you don't care about the position of the word - use random.choice(). If you do, then use random.randint as Cyberhigh has suggested.
5/ you can multiply a string by a number - "*"*4 - or a list by a number - *4
6/ Your finished code should be four lines long (plus blank lines)
random observation: why is it that people are so often drawn to use a list where a string or int will do??
April 15th, 2005, 09:44 PM
yes i know that it would be easier to just use a string instead of a list but for the purpose that i need it for, i need a list. i got it working now. thank all of you for your help