So I have a .txt file which contains the name, abbreviation, nickname, and capital of each state in the United States per line.
I need to print the names of 5 random states on different lines, one below the other. The text file looks like -
Alabama,AL,Cotton State,Montgomery
Alaska,AK,The Last Frontier,Juneau
Arizona,AR,Grand Canyon State,Phoenix
Arkansas,AK,Land of Opportunity,Little Rock
and so on...
I imported the file and put it into a list by splitting at the comma. I have a list of all the names in it. But when I run random.choice(data2) instead of getting a random word, I get a random letter. This means that the list is made of each letter as an element and not each word as an element.
My code -
import random
inflie=open("F:\\SKKU\\study\\ISS3178 - Python\\11\\StatesANC.txt","r")
for line in inflie:
data1=line.split(",")
data2=data1[0]
print(random.choice(data2))
I expect a random state name but what I get is a random letter from one of the state names.
Aucun commentaire:
Enregistrer un commentaire