Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming m CSC 110 Homework 5 Working with Files Project Given the following

ID: 3798986 • Letter: P

Question

Programming m CSC 110 Homework 5 Working with Files Project Given the following file that stores the locations of all modern summer Olympic games Sample Code www.cs.uri.edu cingiser/csc110/assignments/olympics.txt write a program that reads in the file and allows a user to look up the location of the summer Olympics in a particular year. Forum Specifically, the program will: CSC Links 1. Ask the user to enter the name of the data file. Schedule 2. Read the data from the file into two lists one for the years and one for the locations. 3. Ask the user to enter the year for which they are interested in the location of the olympics. Gradebook 4. Print the location of the olympics for that year. University Course a Here are a few things to note when working on this program: Policies 1. In the text file, the data is delimited by a tab so you will need to use the following command to get a line of data from the file Site Info year, loc line. split ("Nt") 2. You should have at least three functions in the program: getData, findLocation and main. Help 3. Be sure to test that the file entered by the user exists and catch the error with exception handling. 4. Be sure to test that the year entered by the user is an integer and catch the error with exception handling. Your output will look something like this: Python 3.5.2 She Python 3.5.2 Cv3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) [GCC 4.2.1 (Apple Inc. build 5666) (dot 301 on darwin Type copyright credits or "licenseO" for more information RESTART: /Users/ldipippo/Documents/csc 110/homework/hw-solutions-fall2016 hw-5-olympics .py main() Please the name of the data file olympics. txx Invalid file name try again Please the name of the data file olympics.txt Enter the year you are interested in: 2004 In 2004 the 0lympics were held in Athens, Greece main() Please the name of the data file olympics txt Enter the year you are interested in 2005 No Olympics were held that year main() Please the name of the data file olympics. txt Enter the year you are interested in 200x Invalid year must be an integer Enter the year you are interested in: 1994 No Olympics were held that year main Please the name of the data fi olympics .txt Enter the year you are interested in: 2016 In 2016 the 0lympics were held in Rio de Janeiro, Brazil Ln: 26 Col: 4

Explanation / Answer

# finding location of olympic game from year of program in Python

def main():
  
return
try:

#getting file name from user
filename = raw_input('Please the name of the data file: ')
fh = open(filename, "r")
except IOError:

#handling exception for wrong file name
print ("Invalid filename try again")
else:

#getting filedata into line
line = fh.readlines()

try:

#reading year value from user
year=int(input('Enter the year you are interested in: '))
except ValueError:
print ("Invalid year - must be integer")
else:
val=1
for row in line:
yr, loc = row.split(" ")

# splitting years into yr list and locations into loc list
yr=int(yr)
if ( year == yr):

#displaying olympics held in location
print "In ",yr," the Olympics were held in ",loc
val=0
if(val==1):

#displaying No olympics held in that year
print "No Olympics were held that year"
fh.close()