Итак, моя программа распечатывает первую часть того, что я хотел бы, но теперь для второго пункта меню я бы хотел, чтобы он нашел покемона по имени, а затем для этого покемона специально заполнил, что такое запись pokedexибо это так. Я хотел бы использовать несколько экземпляров repr (), но пока это не сработало.
edit: я решил попробовать метод поиска, используя:
def lookup_by_name(pokedex, name):
for i in pokedex:
search: name
if i == search:
print(i)
print("yes")
но когда я делаю это, я получаю сообщение об ошибке "локальная переменная 'search', указанная перед присваиванием"
import string
def print_menu():
print("1. Print Pokedex")
print("2. Print Pokemon by Name")
print("3. Print Pokemon by Number")
print("4. Count Pokemon with Type")
print("5. Print Average Pokemon Combat Points")
print("6. Quit")
return print_menu
def print_pokedex(pokedex):
print(pokedex)
def lookup_by_name(pokedex, name):
print(Pokemon.print_pokemon_name(Pokemon))
class Pokemon:
def __init__(self, name, number, combat_points, types):
self.number = number
self.name = name
self.combat_points = combat_points
self.types = types
def __repr__(self):
return "Number: {}, Name: {}, CP: {}, Types: {}, \n".format(str(self.number), self.name, str(self.combat_points), str(self.types).strip("[]',"))
def print_pokemon_name(self):
return "Number: {}, Name: {}, CP: {}, Types: {}, \n".format(str(self.number), self.name, str(self.combat_points), str(self.types).strip("[]',"))
# ---------------------------------------
# Do not change anything below this line
# ---------------------------------------
def create_pokedex(filename):
pokedex = []
file = open(filename, "r")
for pokemon in file:
pokelist = pokemon.strip().split(",")
number = int(pokelist[0]) # number
name = pokelist[1] # name
combat_points = int(pokelist[2]) # hit points
types = []
for position in range(3, len(pokelist)):
types += [pokelist[position]] # type
pokedex += [Pokemon(name, number, combat_points, types)]
file.close()
return pokedex
# ---------------------------------------
def get_choice(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 = create_pokedex("pokedex.txt")
choice = 0
while choice != 6:
print_menu()
choice = get_choice(1, 6, "Enter a menu option: ")
if choice == 1:
print_pokedex(pokedex)
elif choice == 2:
name = input("Enter a Pokemon name: ").lower()
lookup_by_name(pokedex, name)
elif choice == 3:
number = get_choice(1, 1000, "Enter a Pokemon number: ")
lookup_by_number(pokedex, number)
elif choice == 4:
pokemon_type = input("Enter a Pokemon type: ").lower()
total_by_type(pokedex, pokemon_type)
elif choice == 5:
average_hit_points(pokedex)
elif choice == 6:
print("Thank you. Goodbye!")
print()
# ---------------------------------------
main()