Question
I can not understand why this code is not working can you explain. And directions said that I need to create another file, how can
I can not understand why this code is not working can you explain. And directions said that I need to create another file, how can I do that?
The code this:
# Create the dictionary dict_cities = {} file=open('tr.cities.txt', 'r') for line in file: line_list = line.split() city = line_list[0] lat = line_list[1] long = line_list[2] major_city = line_list[3] population = line_list[4] if major_city not in dict_cities: dict_cities[major_city] = [1, int(population)] else: if population != "-1": dict_cities[major_city][0] += 1 dict_cities[major_city][1] += int(population)
# Find the most eastern, most western, most northern, and most southern cities
east_city = "Idr" west_city = "anakkale" north_city = "Sinop" south_city = "Hatay"
east_long = -180 west_long = 180 north_lat = -90 south_lat = 90
for city in dict_cities: if float(long) > east_long: east_long = float(long) east_city = city if float(long) north_lat: north_lat = float(lat) north_city = city if float(lat)
# Print the results print("The most eastern city is", east_city) print("The most western city is", west_city) print("The most northern city is", north_city) print("The most southern city is", south_city)
# Create the output file outfile_name = input("Please enter the name of the output file: ") outfile = open(outfile_name, "w")
for city in dict_cities: outfile.write(city + " " + str(dict_cities[city][0]) + " " + str(dict_cities[city][1]) + " ")
outfile.close()
file.close()
def main(): # Ask the user for an input filename and output filename input_filename = input("Enter an input filename: ") output_filename = input("Enter an output filename: ") # Read the input file cities_file = open(input_filename, "r") # Initialize a dictionary to store the data cities_dict = {} # Loop through each line of the input file for line in cities_file: # Split the line into a list of values values = line.split() # Get the major city name major_city = values[3] # Check if the major city is already in the dictionary if major_city in cities_dict: # If it is, add one to the city count and update the population cities_dict[major_city][0] += 1 cities_dict[major_city][1] += int(values[4]) else: # If it isn't, add it to the dictionary with a city count of 1 and the given population cities_dict[major_city] = [1, int(values[4])] # Find the most eastern, most western, most northern, and most southern cities east_city, east_long = None, -180 west_city, west_long = None, 180 north_city, north_lat = None, -90 south_city, south_lat = None
In this homework assignment, you will work on text files. First download the tr_cities.txt file that we put on Blackboard 1. It contains a list of selected cities in Turkey. Each line contains the following values separated with white space characters: 1. The first value is the city name. 2. The second and third values are the latitude and the longitude of this city, respectively. 3. Each city, which was read as the first value, may be a major city (in Turkish, IL) or a minor city/town (in Turkish, ILE or KASABA) belonging to a major city. The fourth value indicates the name of the major city that this city belongs to. 4. The fifth value is the population of this city. Note that for some small towns, the population information is not available. These populations are indicated with 1. Now implement a program that reads a text file, creates a dictionary of major cities according to the explanations given below, and writes this dictionary into another text file. This program also displays the most eastern, most western, most northern, and most southern cities on the screen. Please read the following carefully before starting your implementation. - Take the input filename from the user. You may assume that a file with this name always exists, and its contents are always valid. Note that tr_cities.txt is just an example file. We may run your program on other files with a similar format, that is, we may use other files that contain different values in its lines. - Create a dictionary of major cities, for which the key is the major city name. There are two values for an item in the dictionary: 1. Number of major/minor cities that this major city has, and 2. Total population of this city, including the populations of all cities that belong to this city. Note that for some small towns, the population information is not available. These populations are indicated with 1. Thus, do not include such cities in calculating the total population. - Write this dictionary into another txt file. Also take the name of this output file from the user. This file should have three values in each of its lines: major city name, city number, and total population. These values should be separated with a space. The first five rows of this output file created for tr_cities.txt are given below. Of course, this output file will contain more lines. IstanbulAnkaraIzmir46Bursa27Antalya204057083683169551341515400057513562888632 Display the most eastern, most western, most northern, and most southern cities on the screen (check the example output given below). To find the most eastern and most western cities, use the longitude information. To find the most northern and most southern cities, use the latitude information. Below are an example run. The first five rows of the output file, called city_summary.txt, are given above. Of course, this output file will contain more lines. Note that we will run your program on other files with a similar format, that is, we may use other files that contain different values in its lines. In [1]: runfile('/Users/hw4/hw4.py', wdir='/Users/hw4') Enter an input filename: tr_cities.txt Most eastern city: Esendere Most western city: Gokceada Most northern city: Sinop Most southern city: Yayladagi Enter an output filename: city_summary.txt Cities are saved WHAT TO SUBMIT? This homework assignment asks you to submit only one file whose name must be your_student_id.py. Use your own student id to give a name to this file. This file should also contain a function called main ( ) and a call to this main function. Like the previous homework assignments, the correctness of your program will affect your grade. However, in addition to this, the following points are important to get full credit. - You MUST use meaningful names for the variables. - You MUST write explanatory and clearly understandable comments. - At the beginning of each file, you MUST write your name, your id, and your section as comments. After these, you MUST provide a very brief description about what the program does as additional comments. For this assignment, you are allowed to use the codes given in our lecture slides. However, you ARE NOT ALLOWED to use any code from other sources (including the codes given in textbooks, found on the Internet, belonging to your classmates, etc.). Do not forget that plagiarism and cheating will be heavily punished. Please do the homework yourself. ***IMPORTANT: You cannot use the csv module in your implementation. This homework will be graded by your TAs, Nafiseh Jabbari Tofighi (ntofighi21@ku.edu.tr), Osman Yasal and Sleyman Yldrm (suleymanyildirim22@ku.edu.tr). Thus, you may directly contact them for your homework-related questions. Before submitting your homework, please be sure that you carefully read all explanations given above and understood them very wellStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started