Skip to content
Advertisement

Random word generator- Python

So i’m basically working on a project where the computer takes a word from a list of words and jumbles it up for the user. there’s only one problem: I don’t want to keep having to write tons of words in the list, so i’m wondering if there’s a way to import a ton of random words so even I don’t know what it is, and then I could enjoy the game too? This is the coding of the whole program, it only has 6 words that i put in:

JavaScript

Advertisement

Answer

Reading a local word list

If you’re doing this repeatedly, I would download it locally and pull from the local file. *nix users can use /usr/share/dict/words.

Example:

JavaScript

Pulling from a remote dictionary

If you want to pull from a remote dictionary, here are a couple of ways. The requests library makes this really easy (you’ll have to pip install requests):

JavaScript

Alternatively, you can use the built in urllib2.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement