In: Computer Science
Assignment
Pokedex.py:
1,bulbasaur,318,grass,poison 2,ivysaur,405,grass,poison 3,venusaur,525,grass,poison 4,charmander,309,fire 5,charmeleon,405,fire 6,charizard,534,fire,flying 7,squirtle,314,water 8,wartortle,405,water 9,blastoise,530,water 10,caterpie,195,bug 11,metapod,205,bug 12,butterfree,395,bug,flying 13,weedle,195,bug,poison 14,kakuna,205,bug,poison 15,beedrill,395,bug,poison 16,pidgey,251,normal,flying 17,pidgeotto,349,normal,flying 18,pidgeot,479,normal,flying 19,rattata,253,normal 20,raticate,413,normal 21,spearow,262,normal,flying 22,fearow,442,normal,flying 23,ekans,288,poison 24,arbok,448,poison 25,pikachu,320,electric 26,raichu,485,electric,psychic 27,sandshrew,300,ground 28,sandslash,450,ground 29,nidoran,275,poison 30,nidorina,365,poison 810,yeet,777,steel,fire,water,grass,electric,psychic,ice,dragon,dark,fairy,???
Transcript:
1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 1 The Pokedex ----------- Number: 1, Name: Bulbasaur, CP: 318: Type: grass and poison Number: 2, Name: Ivysaur, CP: 405: Type: grass and poison Number: 3, Name: Venusaur, CP: 525: Type: grass and poison Number: 4, Name: Charmander, CP: 309: Type: fire Number: 5, Name: Charmeleon, CP: 405: Type: fire Number: 6, Name: Charizard, CP: 534: Type: fire and flying Number: 7, Name: Squirtle, CP: 314: Type: water Number: 8, Name: Wartortle, CP: 405: Type: water Number: 9, Name: Blastoise, CP: 530: Type: water Number: 10, Name: Caterpie, CP: 195: Type: bug Number: 11, Name: Metapod, CP: 205: Type: bug Number: 12, Name: Butterfree, CP: 395: Type: bug and flying Number: 13, Name: Weedle, CP: 195: Type: bug and poison Number: 14, Name: Kakuna, CP: 205: Type: bug and poison Number: 15, Name: Beedrill, CP: 395: Type: bug and poison Number: 16, Name: Pidgey, CP: 251: Type: normal and flying Number: 17, Name: Pidgeotto, CP: 349: Type: normal and flying Number: 18, Name: Pidgeot, CP: 479: Type: normal and flying Number: 19, Name: Rattata, CP: 253: Type: normal Number: 20, Name: Raticate, CP: 413: Type: normal Number: 21, Name: Spearow, CP: 262: Type: normal and flying Number: 22, Name: Fearow, CP: 442: Type: normal and flying Number: 23, Name: Ekans, CP: 288: Type: poison Number: 24, Name: Arbok, CP: 448: Type: poison Number: 25, Name: Pikachu, CP: 320: Type: electric Number: 26, Name: Raichu, CP: 485: Type: electric and psychic Number: 27, Name: Sandshrew, CP: 300: Type: ground Number: 28, Name: Sandslash, CP: 450: Type: ground Number: 29, Name: Nidoran, CP: 275: Type: poison Number: 30, Name: Nidorina, CP: 365: Type: poison Number: 810, Name: Yeet, CP: 777: Type: steel and fire and water and grass and electric and psychic and ice and dragon and dark and fairy and ??? 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 2 Enter a Pokemon name: RAICHU Number: 26, Name: Raichu, CP: 485: Type: electric and psychic 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 2 Enter a Pokemon name: raichump There is no Pokemon named raichump 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 3 Enter a Pokemon number: 26 Number: 26, Name: Raichu, CP: 485: Type: electric and psychic 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 3 Enter a Pokemon number: 34 There is no Pokemon number 34 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 4 Enter a Pokemon type: Normal Number of Pokemon that contain type normal = 7 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 4 Enter a Pokemon type: ??? Number of Pokemon that contain type ??? = 1 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 4 Enter a Pokemon type: unknown Number of Pokemon that contain type unknown = 0 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 5 Average Pokemon combat points = 370.71 1. Print Pokedex 2. Print Pokemon by Name 3. Print Pokemon by Number 4. Count Pokemon with Type 5. Print Average Pokemon Combat Points 6. Quit Enter a menu option: 6 Thank you. Goodbye!
Grading - 100 points
Code:
# -----------------------------
# 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 print_pokedex(pokedex):
print("\n------ The Pokedex --------\n")
for i in pokedex:
list = pokedex.get(i)
n = len(list)
if n == 3:
print("Number = ", i, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2])
if n == 4:
print("Number = ", i, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2] + " and " + list[3])
print()
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 lookup_by_number(pokedex, number):
if number in pokedex:
list = pokedex.get(number)
n = len(list)
if n == 3:
print("Number = ", number, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2])
if n == 4:
print("Number = ", number, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2] + " and " + list[3])
print()
else:
print("Invalid number!\n")
def lookup_by_name(pokedex, name):
found = 0
for i in pokedex:
list = pokedex.get(i)
if list[0].lower() == name.lower():
found = 1
n = len(list)
if n == 3:
print("Number = ", i, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2])
if n == 4:
print("Number = ", i, "Name: ", list[0], "CP: ", list[1] , "Type: " + list[2] + " and " + list[3])
print()
if found == 0:
print("Invalid name!\n")
def total_by_type(pokedex, type):
count = 0
for i in pokedex:
list = pokedex.get(i)
n = len(list)
if n == 4:
if ((list[2].lower() == type.lower()) or (list[2].lower() == type.lower())):
count = count + 1
if n == 3:
if (list[2] == type):
count = count + 1
print("Number of pokemon of given type = ", count)
print()
def average_hit_points(pokedex):
count = 0
for i in pokedex:
list = pokedex.get(i)
count = count + list[1]
avg = count / 30
print("Average hit points = %.2f" %avg)
print()
def print_menu():
print("1. Print Pokedex")
print("2. Print Pokemon by Name")
print("3. Print Pokemon by Number")
print("4. Count pokemon by type")
print("5. Print Average Hit Points")
print("6. Quit")
# -----------------------------
def main():
pokedex = createPokedex("pokedex.txt")
choice = 0
while choice != 6:
print_menu()
choice = getChoice(1, 6, "Enter a menu option: ")
if choice == 1:
print_pokedex(pokedex)
elif choice == 2:
name = input("Enter a Pokemon name: ")
name = name.capitalize()
lookup_by_name(pokedex, name)
elif choice == 3:
number = getChoice(1, 1000, "Enter a Pokemon number: ")
lookup_by_number(pokedex, number)
elif choice == 4:
type = input("Enter a Type: ")
total_by_type(pokedex, type.lower())
elif choice == 5:
average_hit_points(pokedex)
print("Thank you. Goodbye!")
# -----------------------------
main()
Output: