dimanche 27 novembre 2016

Getting a random word from a text file

I am trying to return a word from a text file (so that I can eventually make a game from the word) but right now I get the error

IndexError: string index out of range

this is what my text file looks like

yellow
awesome
barking
happy
dancing
laughing

and this is the code I currently have

import random

def generate_the_word(infile):

    for line in infile.readlines():
        random_line = random.randrange(line[0], line[len(line) + 1])

        print(random_line)



def main():
    infile = open("words.txt","r")
    generate_the_word(infile)

    infile.close


main()

Do I have the wrong idea about indexing?




Aucun commentaire:

Enregistrer un commentaire