# Olympics.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-04-26 File created. # 2022-12-27 Converted to Python 3. # This programs shows how to process objects that are stored # in a tuple. # See notes at the end of this file. class Olympics : def __init__( self, given_olympic_year, given_olympic_city, given_olympic_country ) : self.olympic_year = given_olympic_year self.olympic_city = given_olympic_city self.olympic_country = given_olympic_country def get_year( self ) : return self.olympic_year def print_olympics_data( self ) : print( "\n In %d, Olympic Games were held in %s, %s." % \ ( self.olympic_year, self.olympic_city, self.olympic_country ) ) # The main program begins. # First we create a tuple that contains Olympics objects. # Later on the program will go through this tuple searching # for given year. olympics_table = ( Olympics( 1896, "Athens", "Greece" ), Olympics( 1900, "Paris", "France" ), Olympics( 1904, "St. Louis", "U.S.A." ), Olympics( 1906, "Athens", "Greece" ), Olympics( 1908, "London", "Great Britain"), Olympics( 1912, "Stockholm","Sweden" ), Olympics( 1920, "Antwerp", "Belgium" ), Olympics( 1924, "Paris", "France" ), Olympics( 1928, "Amsterdam","Netherlands"), Olympics( 1932, "Los Angeles", "U.S.A."), Olympics( 1936, "Berlin", "Germany" ), Olympics( 1948, "London", "Great Britain"), Olympics( 1952, "Helsinki","Finland" ), Olympics( 1956, "Melbourne","Australia" ), Olympics( 1960, "Rome", "Italy" ), Olympics( 1964, "Tokyo", "Japan" ), Olympics( 1968, "Mexico City","Mexico" ), Olympics( 1972, "Munich", "West Germany"), Olympics( 1976, "Montreal", "Canada" ), Olympics( 1980, "Moscow", "Soviet Union"), Olympics( 1984, "Los Angeles","U.S.A."), Olympics( 1988, "Seoul", "South Korea"), Olympics( 1992, "Barcelona","Spain" ), Olympics( 1996, "Atlanta", "U.S.A." ), Olympics( 2000, "Sydney", "Australia" ), Olympics( 2004, "Athens", "Greece" ), Olympics( 2008, "Beijing", "China" ), Olympics( 2012, "London", "Great Britain"), Olympics( 2016, "Rio de Janeiro", "Brazil" ), Olympics( 9999, "end of", "data" ) ) print( "\n This program can tell where the Olympic " \ "\n Games were held in a given year. Give " \ "\n a year by using four digits: ", end="" ) given_year = int( input() ) olympics_index = 0 table_search_ready = False while table_search_ready == False : if olympics_table[ olympics_index ].get_year() == given_year : olympics_table[ olympics_index ].print_olympics_data() table_search_ready = True elif olympics_table[ olympics_index ].get_year() == 9999 : print( "\n Sorry, no Olympic Games were held in %d." % given_year ) table_search_ready = True else : olympics_index += 1 # NOTES: # It is known to the author of this program that it is not # an elegant way to mark the end of olympics data with a strange # Olympics object. Modify the program so that you can get rid of # the last Olympics object in the tuple. You can use the len() function # to check whether the whole tuple is procesed. # In an earlier version of this program olympics table # was created in the following way. #olympics_table = [] # This creates an empty list. #olympics_table.append( Olympics( 1896, "Athens", "Greece" ) ) #olympics_table.append( Olympics( 1900, "Paris", "France" ) ) #olympics_table.append( Olympics( 1904, "St. Louis", "U.S.A." ) ) #olympics_table.append( Olympics( 1906, "Athens", "Greece" ) ) #olympics_table.append( Olympics( 1908, "London", "Great Britain") ) #olympics_table.append( Olympics( 1912, "Stockholm","Sweden" ) ) #olympics_table.append( Olympics( 1920, "Antwerp", "Belgium" ) ) #olympics_table.append( Olympics( 1924, "Paris", "France" ) ) #olympics_table.append( Olympics( 1928, "Amsterdam","Netherlands") ) #olympics_table.append( Olympics( 1932, "Los Angeles", "U.S.A.") ) #olympics_table.append( Olympics( 1936, "Berlin", "Germany" ) ) #olympics_table.append( Olympics( 1948, "London", "Great Britain") ) #olympics_table.append( Olympics( 1952, "Helsinki","Finland" ) ) #olympics_table.append( Olympics( 1956, "Melbourne","Australia" ) ) #olympics_table.append( Olympics( 1960, "Rome", "Italy" ) ) #olympics_table.append( Olympics( 1964, "Tokyo", "Japan" ) ) #olympics_table.append( Olympics( 1968, "Mexico City","Mexico" ) ) #olympics_table.append( Olympics( 1972, "Munich", "West Germany") ) #olympics_table.append( Olympics( 1976, "Montreal", "Canada" ) ) #olympics_table.append( Olympics( 1980, "Moscow", "Soviet Union") ) #olympics_table.append( Olympics( 1984, "Los Angeles","U.S.A.") ) #olympics_table.append( Olympics( 1988, "Seoul", "South Korea") ) #olympics_table.append( Olympics( 1992, "Barcelona","Spain" ) ) #olympics_table.append( Olympics( 1996, "Atlanta", "U.S.A." ) ) #olympics_table.append( Olympics( 2000, "Sydney", "Australia" ) ) #olympics_table.append( Olympics( 2004, "Athens", "Greece" ) ) #olympics_table.append( Olympics( 2008, "Beijing", "China" ) ) #olympics_table.append( Olympics( 2012, "London", "Great Britain") ) #olympics_table.append( Olympics( 9999, "end of", "data" ) )