how can i randomly print an element from a list in python

Posted by lm on Stack Overflow See other posts from Stack Overflow or by lm
Published on 2010-05-04T02:00:58Z Indexed on 2010/05/04 2:08 UTC
Read the original article Hit count: 246

Filed under:
|
|

So far i have this, which prints out every word in my list, but i am trying to print only one word at random. Any suggestions?

def main():
    # open a file
    wordsf = open('words.txt', 'r')
    word=random.choice('wordsf')
    words_count=0
    for line in wordsf:
        word= line.rstrip('\n')
        print(word)
        words_count+=1      

    # close the file
    wordsf.close()

© Stack Overflow or respective owner

Related posts about python

Related posts about random