# ----------------------------- # The Joy and Beauty of Data # Assignment 3: Pokedex # Your Name: # Date: # ----------------------------- import string # ----------------------------- def createPokedex(filename): pokedex = {} file = open(filename, "r") for pokemon in file: pokelist = pokemon.strip().split(",") index = int(pokelist.pop(0)) pokedex[index] = [pokelist.pop(0)] # name pokedex[index] += [int(pokelist.pop(0))] # hit points pokedex[index] += [pokelist.pop(0)] # type if len(pokelist) == 1: pokedex[index] += [pokelist.pop(0)] # optional second type return pokedex # ----------------------------- def getChoice(low, high, message): legal_choice = False while not legal_choice: legal_choice = True answer = input(message) for character in answer: if character not in string.digits: legal_choice = False print("That is not a number, try again.") break if legal_choice: answer = int(answer) if (answer < low) or (answer > high): legal_choice = False print("That is not a valid choice, try again.") return answer # ----------------------------- def main(): pokedex = createPokedex("pokedex.txt") choice = 0 while choice != 6: printMenu() choice = getChoice(1, 6, "Enter a menu option: ") if choice == 1: printPokedex(pokedex) elif choice == 2: name = input("Enter a Pokemon name: ") name = name.capitalize() lookupByName(pokedex, name) elif choice == 3: number = getChoice(1, 1000, "Enter a Pokemon number: ") lookupByNumber(pokedex, number) elif choice == 4: howManyPokemon(pokedex) elif choice == 5: howManyHitPoints(pokedex) print("Thank you. Goodbye!") # ----------------------------- main()