#1
  1. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    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.

    Code:
    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:
          blanks.append('_')
          x = x + 1
    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.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I've got to get going so I'l make this brief.
    import random
    list = ['a', 'b', 'c,'...
    print random.choice(list)

    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    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

    Code:
    import random
    r = random.randint(1,3)
    print r
    theres an extremly simple example. Remember this is by no means suitible for cryptography purposes, so please do not use it as such.
    but a great info page about the module random can be found on pythons web site, http://docs.python.org/lib/module-random.html
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by lw22
    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.
    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.

    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 - [1]*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??

    --OH.
  8. #5
  9. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    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

IMN logo majestic logo threadwatch logo seochat tools logo