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

File Edit View History Bookmarks People Window Help Inbox (524 matthew marwel x

ID: 3798735 • Letter: F

Question

File Edit View History Bookmarks People Window Help Inbox (524 matthew marwel x SakalaURI: csc 110 Spring x c chegg study l Guided solutio x l Secure https//sakai.uri.edu/portal/site/e3dbb580-16f5-4 Given the following file that stores the locations of all modern summer olympic games: 10 de La write a program that readsin the fie and alows avser to took up the location of the summer olympics na partautar year specifically, the program wil 1. Ask the user to enter the name of the data file. 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. 4. Print the location of the olympics for that year Here are a few things to note when working on this program: 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 year, loc line split ("It" 2. You should have at least three functions in the program: getData, findLocation and main. 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.

Explanation / Answer

#!/usr/bin/python

import os

year = []
place = []

#In this function the program checks the existence of the file and jumps to the except block if the file doesn't exist
#fileob.read() reads the entire file into the "str" variable
#lines is a list which contains individual lines from the files . This is obtained by splitting "str" by " "
#Further info is extracted by spliting individual lines by " "
def getData(filename):
   try:
       os.path.exists(filename)
       fileob = open(filename, "r")
       linecount = 0
       str = fileob.read()
       lines = str.split(" ");
  
       for val in lines:
           if not val:
               break

           info = val.split(" ");
           year.insert(linecount, info[0]);
           place.insert(linecount, info[1]);
           linecount +=1

       return 0
   except IOError:
       print "Invalid file name try again ..."
       return -1


def findLocation(input_year):
   count = 0
   for itr in year:
       if int(itr) == input_year:
           print "In " ,itr, " the Olympics were held in " , place[count]
           return
       count += 1
   print "No Olympics were held that year"
   return
      

def main():
   filename = raw_input("Please the name of the data file: ")
   ret = getData(filename)

   if ret != 0:
       return

   while(1):
       try:
           input_year = int(raw_input("Enter the year you are interested in:"))
           break
       except ValueError:
           print "Invalid year - must be an integer"
           continue

   findLocation(input_year)

while(1):
   main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote