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()
If words.txt look like
first
second
change your code to:
import random
def a_word():
file = open('words.txt', 'r')
random_word = random.choice(file.readlines())
print('The %s tree.' % random_word)
return
a_word()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.