|
| 1 | +#GEOG 592 Lab 2 |
| 2 | +#Question 1 |
| 3 | +# Ask a user to input the radius value for a circle, |
| 4 | +# Prompt the user with its area and perimeter values |
| 5 | +# (Used try-except for error handling, while loop) |
| 6 | +from math import pi |
| 7 | +#while(True): keeps the loop running infinitely |
| 8 | +while(True): |
| 9 | + try: |
| 10 | + radius_circle = float(input('The radius of the circle in cm: ')) |
| 11 | + break #needed to be added to come out of the loop |
| 12 | + except ValueError: |
| 13 | + print('Invalid Format! Input a number!') |
| 14 | + |
| 15 | +def area_peri_circle(): |
| 16 | + area_circle = pi*radius_circle**2 |
| 17 | + perimeter_circle = 2*pi*radius_circle |
| 18 | + print('The area of the circle is', format(area_circle, '.2f'), 'cm².','\nThe perimeter of the circle is',format(perimeter_circle, '.2f'), 'cm.') |
| 19 | +area_peri_circle() |
| 20 | +# function is defined at the beginning and called at the end |
| 21 | + |
| 22 | + |
| 23 | +#Question 2 |
| 24 | +#Ask user to input a list of numbers, separated by space, |
| 25 | +#Prompt the user with the average, median, sum of the list |
| 26 | +#Output a sorted list. |
| 27 | +# (Added error handling and while loop) |
| 28 | +while(True): |
| 29 | + try: |
| 30 | + user_input = input('Input a list of numbers separated by space: ') |
| 31 | + numbers_list = list(map(float, user_input.split(" "))) |
| 32 | + break #needed to be added to come out of the loop |
| 33 | + except ValueError: |
| 34 | + print('Invalid Format! Try again!') |
| 35 | +#Calculating the sum |
| 36 | +len = len(numbers_list) # counts the number of numbers in the list |
| 37 | +sum = 0 |
| 38 | +for i in numbers_list: |
| 39 | + sum += i # sum = sum + i |
| 40 | +print(f'The sum of the numbers in the list: {sum}') |
| 41 | +#Calculating the average |
| 42 | +print(f'The average of the numbers in the list: {sum/ len}') |
| 43 | +#Calculating the median |
| 44 | +numbers_list.sort() |
| 45 | +if len % 2 == 0: |
| 46 | + median1 = numbers_list[len//2] |
| 47 | + median2 = numbers_list[(len // 2)-1] |
| 48 | + median = (median1 + median2)/2 |
| 49 | +else: |
| 50 | + median = numbers_list[len//2] |
| 51 | +print(f'The median of the numbers in the list: {median}') |
| 52 | + |
| 53 | +#Question 3 |
| 54 | +ncFile = open('NC2010.txt', 'r') #opening a file for the pupose of reading it i.e file 1 |
| 55 | +allLines = ncFile.readlines()[1:] #skipping the first line(header) and reading all lines in ncFile i.e file 1 |
| 56 | +file = open('ncpopchg.txt','w') #opening a new fie for the purpose of writing in it i.e file 2 |
| 57 | +for aLine in allLines: #working with ncFile ('NC2010.txt') first i.e. file 1 |
| 58 | + aListTokens = aLine.split(',') # the split function performs the split based on the comma |
| 59 | + aListTokens[6], aListTokens[8] = float(aListTokens[6]), float(aListTokens[8]) #changing values to float |
| 60 | + pop_change = ((aListTokens[8]-aListTokens[6])/aListTokens[8])*100 #calculation |
| 61 | + file.write(aListTokens[1]+',') #writing to the created file i.e file 2 ('ncpopochang.txt') |
| 62 | + file.write(str(pop_change)+'%'+'\n') |
| 63 | +ncFile.close() #closing file 1 |
| 64 | +file.close() #closing file 2 |
| 65 | + |
| 66 | +#Question 4 |
| 67 | +ncFile2 = open('NC2010.txt', 'r') ##opening a file for the pupose of reading it i.e file 1 |
| 68 | +allLines2 = ncFile2.readlines()[1:] #skipping the first line(header) of file 1 and reading its content |
| 69 | +file2 = open('ncpopchgSorted.txt','w') #opening a new file for the purpose of writing in it i.e file 2 |
| 70 | +county_list = [] #creating an empty list |
| 71 | +for aLine2 in allLines2: #working with file 1 |
| 72 | + aListTokens2 = aLine2.split(',') # the split function performs the split based on the comma |
| 73 | + aListTokens2[6], aListTokens2[8] = float(aListTokens2[6]), float(aListTokens2[8]) #chaning values to float |
| 74 | + pop_change2 = ((aListTokens2[8]-aListTokens2[6])/aListTokens2[8])*100 #calculation |
| 75 | + county_list.append((aListTokens2[1],pop_change2)) #adding the values of file 1 to the empty list (county_list) using the append function |
| 76 | +county_list.sort(key=lambda x:x[1]) #sorting the appended list (county_list) |
| 77 | +for list_2 in county_list: #county_list has two columns |
| 78 | + alinestring = list_2[0]+','+str(list_2[1])+'%'+'\n' #formatting the two colums of county_list and using it to form 'alinestring' variable |
| 79 | + file2.write(alinestring) #writing variable to file 2 (the file that was opening for the purpose of writing into it) |
| 80 | + |
| 81 | +ncFile2.close() #closing file 1 |
| 82 | +file2.close() # closing file 2 |
| 83 | + |
| 84 | +#Question 5 |
| 85 | +#Part a)calculating the mean center of both list |
| 86 | +list1 =[(10,20),(25,8),(34,22),(17,35),(9,1),(31,20),(44,11)] #list 1 of coordinate points |
| 87 | +list2 = [(1,21),(19,22),(23,12),(51,26),(78,61),(41,17),(11,21),(81,10),(79,51)] #list 2 of coordinate points |
| 88 | +combined_list = list1 + list2 #adding list 1 and list 2 together |
| 89 | +def mean_center(tList): #defining the mean center function |
| 90 | + middle_point = [0,0] #setting the median point value |
| 91 | + n = 0 |
| 92 | + for p in tList: |
| 93 | + middle_point[0] += p[0] |
| 94 | + middle_point[1] += p[1] |
| 95 | + n+=1 |
| 96 | + x = middle_point[0]/n |
| 97 | + y = middle_point[1]/n |
| 98 | + print('%.2f'%x, '%.2f'%y) |
| 99 | +mean_center(list1) #mean center of list 1 |
| 100 | +mean_center(list2) #mean center of list 2 |
| 101 | +mean_center(combined_list) ##mean center of list 1 and 2 |
| 102 | + |
| 103 | +#part b) finding the average distance between a point in list1 and a point in list 2 |
| 104 | +total_dist = 0 |
| 105 | +for point1 in list1: |
| 106 | + for point2 in list2: |
| 107 | + dist = ((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)**.5 #calculating the distance of the points |
| 108 | + total_dist += dist #incrementing the distance calculated above into total_dist (was intially set to 0) |
| 109 | +avg_dist = total_dist/(len(list1) * len(list2)) |
| 110 | +print('The total distance is', '%.2f'%total_dist) |
| 111 | +print('The average distance between a point in list 1 and a point in list 2 is', '%.2f'%avg_dist) |
| 112 | + |
| 113 | +#Question 6 |
| 114 | +city_file = open('cities.txt','r') #opening a text file for the pupose of reading it i.e file 1 |
| 115 | +location_file = open('locations.txt','r') #opening a text file for the pupose of reading it i.e file 2 |
| 116 | +all_cities = city_file.readlines()[1:] #reading file 1, excluding the header |
| 117 | +all_locations = location_file.readlines()[1:] #reading file 2, excluding the first line, which is the header |
| 118 | +location_list = [] #creating an empty list |
| 119 | + |
| 120 | +for aloc in all_locations: #reading file 2 |
| 121 | + alocArgList = aloc.split(',') #spliting the contents of file 2 |
| 122 | + location_ID = alocArgList[0] #calling a column |
| 123 | + location_x = float(alocArgList[1]) #calling a column and tranforming its values to float |
| 124 | + location_y = float(alocArgList[2]) #calling a column and tranforming its values to float |
| 125 | + |
| 126 | + total_distance = 0 |
| 127 | + for aCity in all_cities: #reading file 1 |
| 128 | + acityArgList = aCity.split(',') #spliting the contents of file 1 |
| 129 | + city_name = acityArgList[0] #calling a column |
| 130 | + city_x = float(acityArgList[1]) #calling a column and tranforming its values to float |
| 131 | + city_y = float(acityArgList[2]) #calling a column and tranforming its values to float |
| 132 | + distance_calc = ((city_x-location_x)**2 + (city_y-location_y)**2)**.5 #distance calculation |
| 133 | + total_distance += distance_calc #incrementing the distance calculated above into total_distance (was intially set to 0) |
| 134 | + total_distance_miles = (total_distance/5280) # Converting feet to miles using 1 mile = 5280 feet |
| 135 | + location_list.append((location_ID, total_distance_miles)) #appending two columns into a an empty lise |
| 136 | +location_list.sort(key = lambda x:x[1]) #sorting a list |
| 137 | +location_list[0][0] |
| 138 | +#print(location_list) |
| 139 | +print('Best location ID =' , location_list[0][0],'.','Average Distance (miles) from all cities =', '%.2f'%location_list[0][1] ) |
0 commit comments