How to call API then list the names into a text file?

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)

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.