python - Random and list slicing working strangely with online world list -


i don't know how use python interact internet. can't seem find beginner learning besides codeacademy course.

anyway wanted try , make hangman game randomly selected word english dictionary. need world list have not definitions. can print out entire world list things aren't acting right when try select specific portion of or run through random.choice()

import urllib import random webpage = urllib.urlopen("http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsen.txt").read() print random.choice(webpage) 

is have tried printing webpage[0: ] or webpage[0: 10] or random number , first doesn't work @ second prints 3 words or 3 pieces of words on separate lines.

each of words on separate line in file, when open url large string of words new line characters. around need split string separate lines.

luckily python has inbuilt methods things in case if add:

webpage = webpage.splitlines() 

between 3rd , 4th line work, resulting in following code:

import urllib import random webpage = urllib.urlopen("http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsen.txt").read() webpage = webpage.splitlines() # split text lines. print random.choice(webpage) 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -