# Analyzing Census Data using Python Assignment Help

In this assignment, you should read files on the census of people used to elect the 435 men of the US House of Representatives. The election of these members is based on the calculation of multipliers and priorities organized by the state. Our Python assignment helpers have written functions to read the files, calculate multipliers, assign priorities and calculate the number of representatives per state.

## Calculating Multipliers and Assigning Priorities

In order to solve this problem, first, we need to create a function open_file that will request to the user the name of the file and try to open it. If the file is successfully opened, the function will return the file object. The next step is to create a function that will calculate the multipliers according to the following formula Next, we will create a function read_file_make_priorities that will receive a file object and a list of multipliers, and then it will read the file, and based on the data and the multipliers given, it will calculate the multipliers. The following step is to create a function add_to_state that will receive a list containing the name of the state and the count of seats and will increment the number of seats by 1 unit. Finally, we create a function to display all the data calculated.

```# -*- coding: utf-8 -*- """ Created on Sun Mar 22 14:55:16 2020 @author: Eduardo """ ''' Your header goes here ''' import math import os.path import csv def open_file(): ''' This funtion takes no argument Prompt the user for a file name and returns a file pointer ''' valid_option = 0 f = None while(valid_option == 0): file_name = input("Please enter the name of the file: ") try: f = open(file_name, "r+") valid_option = 1 except IOError: continue return f def calc_multipliers(): ''' This function returns a list of multipliers for values of n from 2 to 60 ''' ret = list() # list to be returned by the function for n in range(2,61): # from 2 to 60 ret.append((1/math.sqrt(n*(n-1)))) return ret def calc_priorities(s,p,m): ''' This function returns a list of priorities for the state. Each element in the list is a tuple with the priority value and the state na,e Arguments: s: Name of the state (str) p: population (int) m: list of multipliers ''' ret = list() N = len(m) for i in range(N): mult = m[i] ret.append((int(p*mult), s)) return ret def read_file_make_priorities(fp,multipliers): ''' This function takes a file pointer and a list of multipliers and inputs Returns two lists: states_reps: a list containing lists, of the form of [state, count] priorities: a list of tuples of the form (priority, state) ... ... containing the priority value and the state name, for each state ''' # Skip header fp.readline() # Read each row of the csv file reader = csv.reader(fp) state_reps = list() priorities = list() for row in reader: state = row.replace('"', '') # state name if state in ['Puerto Rico', 'District of Columbia']: # skip these states continue population = int(row) child_list = [state, 1] state_reps.append(child_list) priorities.extend(calc_priorities(state, population, multipliers)) # Sort the lists state_reps.sort(key = lambda x: x) priorities.sort(key = lambda x: x, reverse=True) return state_reps, priorities def add_to_state(state,states): ''' This function receives a state name and a list of lists of the form [state, count] For each state inside the list, the count is increased by 1 This function returns nothing ''' for child_list in states: if child_list == state: child_list += 1 def display(states): ''' This function receives a list of lists where each list is of the form [state, count] and then prints the state name and its cound ''' print("{:<15s} {:>4s}".format("State", "Representatives")) for child_list in states: print("{:<15s} {:>4s}".format(child_list, str(child_list))) def main(): ''' This function calls all the above functions. Ask the user for a file name, calculates multipliers, and then priorities for each state. Finally, prints the states and their representatives This function returns nothing ''' file = open_file() multipliers = calc_multipliers() state_reps, priorities = read_file_make_priorities(file, multipliers) seats = 385 # Total number of seats for tup in priorities: add_to_state(tup, state_reps) seats -= 1 if seats == 0: break display(state_reps) file.close() # close the data file if __name__ == "__main__": main() ```