I'm doing this project and it needs to take input from names from the https://www.ssa.gov/oact/babynames/limits.html , so i created a loop to get all the names in the file and then made another to check for the largest name. However the loop only seems to run through once and gives the first name in the list. Is there a way to make it so it goes through the list?
## https://www.ssa.gov/oact/babynames/limits.html
file = open('yob2003.txt', 'r')
# variables to store the most popular name
# and the most popular name count
most_pop_name = ""
allNames = set()
count = 0
for line in file: # loop to check for names
list = line.split(',')
if int(list[2]) > 100: # not worth to work with stuff under 100
allNames.add(list[0])
print(len(allNames))
bigList = sorted(allNames)
file.close()
file = open('yob2003.txt', 'r')
for x in range(len(bigList)): # big loop that goes through once
total = 0
for line in file:
list = line.split(',')
if bigList[x] == list[0]:
total += int(list[2])
if total > count:
most_pop_name = str(list[0])
total = count
# print most popular name
print("Most popular name in [NJ] :: " + str(most_pop_name) + " " + str(count))
fileis an iterator. After you iterated it in your first loop, it is exhausted. Open the file anew or reset it to the first position withseek(0)