Ask coding questions

← Back to all posts
How to run random word on txt file
Leroy01010

I have python code and want to print a random word of a txt file how do we do this?

Answered by ch1ck3n [earned 5 cycles]
View Answer
Voters
Leroy01010
Comments
hotnewtop
SixBeeps

There are a bajillion ways of doing this, but one way is to read the file, use split() to separate the data into a word list, then use random.choice() to select a random word.

Leroy01010

ok thanks ill test it out @SixBeeps

Leroy01010

wait i don't understand where to put it @SixBeeps

ch1ck3n

@Leroy01010

main.py:

hi.txt:

Leroy01010

worksed @ch1ck3n

Spacecraft
Leroy01010

worked @Spacecraft

Leroy01010

crashed how do i split the text from the other @Spacecraft