mardi 24 décembre 2019

Random word generator from external file in Python

I'm trying to create a small program to allocate words randomly to describe a tree. For some reason only the first letter of the word is printed. The "words.txt" file contains ~ 3000 adjectives. Please advise on how to print the full word. I've been trying to figure this out for a while and cannot find a solution.

Here is the code:

import random


def a_word():
    file = open('words.txt', 'r')
    random_word = random.choice(file.readline())
    print('The %s tree.' % random_word)
    return


a_word()



Aucun commentaire:

Enregistrer un commentaire