Need to open text file, print random word with over 5 characters python -
import random dictionary = open('word_list.txt', 'r') line in dictionary: in range(0, len(line)): if >= 5: word = random.choice(line) dictionary.close()
- this code doesnt seem work me
- here link file if helps http://vlm1.uta.edu/~athitsos/courses/cse1310_summer2013/assignments/assignment8/word_list.txt
import random open('word_list.txt', 'r') f: words = [word.rstrip() word in f if len(word) > 5] print random.choice(words)
as @ashwini-chaudhary correctly pointed out, word
on each step of iteration has newline \n
@ end - that's why need use rstrip()
.
Comments
Post a Comment