I am trying to create a counter that finds what it needs to count from one file and what it should count from another file. It opens file1 and finds a city and its population separated by a dash, and file2 shows a city name and the crime separated by a dash. When I hardcode the city name in it works fine, but when I try and do it using an if loop to find the city name, it will find how many times the first city comes up in the crime report, but no more after that. Help please
for line in file1:
dash = line.find("-")
variableCity = line[:dash]
cityPop = line[dash + 1:]
crimeCounter = 0
for crime in file2:
x = crime[:dash]
if x == variableCity:
crimeCounter += 1
print("{} which has a population of {} has {} reported crimes".format(variableCity, cityPop, crimeCounter))
that is my code
file1:
Bothell-89232
Kent-97232
Tacoma-89333
Renton-98632
Redmond-64789
Seattle-76978
file2:
Kent-Theft
Tacoma-Break In
Seattle-Break In
Tacoma-Auto Break In
Federal Way-Auto Break In
Kent-Break In
Tacoma-Auto Break In
Federal Way-Auto Break In
Kent-Mugging
Kent-Break In
Federal Way-Break In
Renton-Break In
Renton-Auto Theft
Tacoma-Mugging
Seattle-Theft
Auburn-Auto Theft
Renton-Theft
Tacoma-Auto Theft
Kent-Mugging
Seattle-Auto Break In
Tacoma-Theft
Kent-Auto Theft
Seattle-Break In
Auburn-Mugging
Tacoma-Mugging
Auburn-Auto Theft
Auburn-Auto Theft
Seattle-Auto Theft
Federal Way-Mugging
Kent-Mugging
Renton-Auto Theft
Tacoma-Mugging
Auburn-Theft
Seattle-Auto Break In
Auburn-Mugging
Seattle-Theft
Auburn-Theft
Auburn-Auto Break In
Federal Way-Auto Break In
Seattle-Break In
Kent-Theft
Seattle-Auto Break In
Federal Way-Auto Break In
Kent-Auto Break In
Seattle-Auto Break In
Renton-Auto Break In
Kent-Auto Break In
Renton-Break In
Federal Way-Mugging
Seattle-Mugging
Renton-Mugging
Renton-Auto Break In
Tacoma-Mugging
Tacoma-Auto Theft
Seattle-Auto Break In
Kent-Auto Theft
Kent-Auto Theft
Federal Way-Mugging
Tacoma-Auto Theft
Federal Way-Theft
Tacoma-Auto Theft
Renton-Auto Theft
Seattle-Theft
Seattle-Auto Break In
Tacoma-Mugging
Tacoma-Auto Theft
Seattle-Break In
Federal Way-Theft
Seattle-Auto Break In
Auburn-Auto Break In
Auburn-Auto Break In
Tacoma-Break In
Seattle-Mugging
Renton-Theft
Auburn-Theft
Renton-Theft
Seattle-Auto Theft
Auburn-Mugging
Seattle-Break In
Kent-Mugging
Kent-Break In
Federal Way-Break In
Federal Way-Auto Theft
Auburn-Theft
Tacoma-Theft
Kent-Auto Break In
Auburn-Auto Theft
Seattle-Mugging
Kent-Theft
Kent-Mugging
Kent-Auto Break In
Seattle-Theft
Tacoma-Auto Theft
Renton-Theft
Renton-Break In
Auburn-Break In
Renton-Mugging
Renton-Mugging
Tacoma-Break In
Please note that in each file the next city appears on a new line