I tried to write a list of the name of pokemon moves when inputting the id of pokemon number which will call the API. However it is doing what I wanted in the output channel in Python which writes a list of moves however it is not in the text file which only lists the last item on the list of move. Below is my code, does someone know what is the problem here? I might have done something wrong.
pokemon_number = input("What is the Pokemon's ID? ")
url = 'https://pokeapi.co/api/v2/pokemon/{}/'.format(pokemon_number)
response = requests.get(url)
print(response)
pokemon = response.json()
pp(pokemon)
print(pokemon['name'])
moves = pokemon ['moves']
for move in moves:
print(move['move']['name'])
name_moves= "{pokemon}, {moves}".format(
pokemon = pokemon ['name'],
moves = (move['move'] ['name'])
)
with open('pokemon.txt', 'a') as pokemon_file:
pokemon_file.write (name_moves + '\n')
moves.append(name_moves)
with open('pokemon.txt', 'a') as pokemon_file:
pokemon_file.write (name_moves + '\n')
print(response.status_code)