vendredi 15 mai 2020

Random word generator in Python

I have written the below code to try and generate a random word of three letters or more from the brisih-english text file I found on my linux machine. I have copied the file to the same directory as my .py file. When I run the program in the terminal, I get no output, but the program seems to run infinitely. I am still a beginner at this, but this seems like such a simple program that I wonder what I could be missing. I already ran some tests to print different elements of the list to make sure I could access the words, which worked fine.

import random

#list contains 101,825 elements
dict = open("british-english")
word_list = dict.readlines()

prompt = ""

def generate():
    global dict
    i = random.randint(0, 101824)
    prompt = word_list[i]
    return prompt

while len(prompt) < 3:
    generate()

print(prompt)



Aucun commentaire:

Enregistrer un commentaire