I'm trying to create a program that is able to randomly choose a name/ word from an external list such as .txt files however, I don't know how to import variables from an external list.
Thank You
I'm trying to create a program that is able to randomly choose a name/ word from an external list such as .txt files however, I don't know how to import variables from an external list.
Thank You
Pretty simple.
import random
inp = open("path/to/your/file/file.txt")
lines=inp.read().split("\n")
nLines = len(lines)
index = int(random.random()*nLines)
inp.close()
randLine = lines[index]
print(randLine)
now depending on the format of your input file you might need to parse things a little differently but this is an example for just grabbing a random line of text from a file.
Edit: as _mad
points out you can use random.choice()
import random
inp = open("path/to/your/file/file.txt")
lines=inp.read().split("\n")
inp.close()
randLine = random.choice(lines)
print(randLine)