vendredi 8 mars 2019

Python - Random.Choice() is returning entire string

I am trying to write a program that queries a word document and returns a line randomly from that document. Currently, every line from the document is being returned, even when I apply random.choice(). Any thoughts on how I can correct this?

I ran a len() on my list and confirmed it is returning multiple variables, but the entire list is still being returned.


 import random

 filename = 'happy document.txt'
 dictionary_list = []

 with open(filename) as file_object:
 lines = file_object.readlines()
   for l in lines: 
     lines = [l.strip('\n') for l in lines]
     dictionary_list.append(lines)

random_choice = random.choice(dictionary_list)
print(random_choice)




Aucun commentaire:

Enregistrer un commentaire