Getting the data - Tom Kleen



DictionariesSkills needed: dictionariesProgram Name: world_series_dict.pyWrite a Python program to read a file of World Series winners and then respond to queries about the data.Getting the dataThe file is an ordinary text file, worldseries.txt, and has one team name per line. Line 1 corresponds to the 1903 World Series and subsequent lines refer to subsequent years. Note that there is still a line for those years in which there was no World Series (1904 and 1994). Read the entire file into two dictionaries. The first dictionary will use the year as the key and the team name as the data, so if you know the year, you can find the winner.The second dictionary will use the team as the key and the number of World Series wins as the data. As you read a new World Series winner from the file there are two possibilities. (1) If you have not previously read this team name, add it to the dictionary (as a key) and set its data to value to 1. (2) If you have previously read this team name, it will already be in the dictionary and you just need to increment its count. The user may enter a team name in either upper- or lower-case (and any combination thereof) and your program should still work. Remember to remove the newline character from the end of the string (rstrip() method).Responding to queriesAfter creating your two dictionaries, repeatedly ask the user to enter a year. If the year is an entry in the dictionary, return the name of the World Series winner for that year, like this: The winner in 1961 was the New York Yankees. Continue to ask the user for a year until he enters an empty string (note that you will not be able to convert it to an int right away because you need to check to see if it is an empty string or a valid integer first). We will not assume that the user will always enter a valid integer. If text is entered that is not a number (check out Python's isnumeric() string method), display an appropriate error message and let the user try again. Do NOT try to convert it to an integer because that will throw an error. If the input is a valid number, but is not a key in the dictionary, display an appropriate error message (e.g. "No World Series was held in 1776.") and let the user try again.When the user is done with the first round of queries (enters an empty string, begin a second round where you repeatedly ask the user to enter the name of a team. If the team is an entry in the dictionary, respond with the number of wins for that team. To simplify things, we will not worry about the case of the text. We will assume that the user always enters the team name exactly as it is in the dictionary (i.e., each word is capitalized). If the team is not in the dictionary, like the Briar Cliff Chargers, respond with "The Briar Cliff Chargers have never won the World Series." Continue to ask the user for a team until he enters the empty string. At this point, your program should end.Sample sessionBelow is a brief interactive session from my program. I hit the Enter key after the 1961 query and again at the end of the program. Enter a year: MCMLXIMCMLXI is not a valid number.Enter a year: 1776No World Series was held in 1776.Enter a year: 1961The winner in 1961 was the NEW YORK YANKEES.Enter a year: Enter a team: Briar Cliff ChargersThe Briar Cliff Chargers have never won the World Series.Enter a team: New York YankeesThe New York Yankees have won the World Series 27 times.Enter a team: MINNESOTA TWINSThe MINNESOTA TWINS have won the World Series 2 times.Enter a team: Done! ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download