pokemon

py

School

University Of Arizona *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

3

Uploaded by MateClover22302

Report
""" File: pokemon.py Author: Nick Brobeck Course: CSC 120, Spring 2024 Purpose: This program reads Pokemon data from a CSV file, organizes it by type, calculates average values for each type's properties, and responds to user queries about Pokemon properties by printing the types with the highest average values. """ def read_pokemon_data(filename): """ Read Pokemon data from a CSV file and organize it by type. Parameters: - filename: Name of the CSV file containing Pokemon data. Returns: A dictionary with Pokemon types as keys, each mapping to a dictionary containing Pokemon names and their respective properties. """ pokemon_data = {} with open(filename, 'r') as file: for line in file: if line.startswith('#'): continue data = line.strip().split(',') pokemon_type = data[2] if pokemon_type not in pokemon_data: pokemon_data[pokemon_type] = {} name = data[1] stats = { 'TOTAL': int(data[4]), 'HP': int(data[5]), 'ATTACK': int(data[6]), 'DEFENSE': int(data[7]), 'SPECIALATTACK': int(data[8]), 'SPECIALDEFENSE': int(data[9]), 'SPEED': int(data[10]) } pokemon_data[pokemon_type][name] = stats return pokemon_data def compute_average_values(pokemon_data): """ Calculate average values for each type's properties. Parameters: - pokemon_data: Dictionary containing Pokemon data organized by type. Returns: A dictionary with Pokemon types as keys, each mapping to a dictionary containing average values for their respective properties. """
average_values = {} for pokemon_type, pokemons in pokemon_data.items(): averages = {} number_of_pokemons = len(pokemons) for stat in pokemons.values(): for key, value in stat.items(): if key not in averages: averages[key] = 0 averages[key] += value for key in averages: averages[key] /= number_of_pokemons average_values[pokemon_type] = averages return average_values def compute_max_averages(average_values): """ Calculate maximum average values for each property. Parameters: - average_values: Dictionary containing average values organized by type. Returns: A dictionary with property names as keys, each mapping to the maximum average value across all Pokemon types. """ max_averages = {} for stat in ['TOTAL', 'HP', 'ATTACK', 'DEFENSE', 'SPECIALATTACK', 'SPECIALDEFENSE', 'SPEED']: # Find the Pokemon type with the highest average for the current # property max_average = max(average_values.values(), key=lambda x: x[stat])[stat] max_averages[stat] = max_average return max_averages def find_pokemon_types(average_values, max_averages): """ Find Pokemon types with the highest average values for each property. Parameters: - average_values: Dictionary containing average values organized by type. - max_averages: Dictionary containing maximum average values for each property. Returns: A dictionary with property names as keys, each mapping to a list of Pokemon types with the highest average value for that property. """ max_averages_types = {} for stat, max_average in max_averages.items(): types_with_max_average = [] for pokemon_type, averages in average_values.items(): if averages[stat] == max_average: types_with_max_average.append(pokemon_type)
max_averages_types[stat] = types_with_max_average return max_averages_types def main(): """ Main function to execute the Pokemon data analysis program. """ input_file = input() pokemon_data = read_pokemon_data(input_file) average_values = compute_average_values(pokemon_data) max_averages = compute_max_averages(average_values) index = find_pokemon_types(average_values, max_averages) u_stat = True while u_stat != "": u_stat = input().strip().upper() if u_stat not in index: break temp_types = index[u_stat] temp_types.sort() for x in temp_types: temp_stat = average_values[x][u_stat] # Print Pokemon types and their corresponding average values print(f"{x}: {temp_stat}") main()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help