How do I see how much the function returns Python

Asked

Viewed 255 times

-1

I made a script that returns the names of online players of a given game only that I wanted to tbm put the quantity. How would this structure for quantity return? I already tried to create a list and with it use the function Len plus it error.

import requests
import json

def Expert_serve():

    serve = "expert"
    url = requests.get('http://infinite-flight-public-api.cloudapp.net/v1/Flights.aspx?apikey=78879b1d-3ba3-47de-8e50-162f35dc6e04&sessionid=7e5dcd44-1fb5-49cc-bc2c-a9aab1f6a856')
    response = url.json()
    for i in range(100):              
        if "IFATC" in response[i]["DisplayName"]:
            print(response[i]['DisplayName'])



teste = (Expert_serve())

print ('Jogadores online {}'.fotmat (len(teste))

inserir a descrição da imagem aqui

1 answer

3


This is because your program is not returning players, just printing their names. You can assemble a list (in the example done with list comprehension), use it as function return, then get its size:

import requests
import json

def Expert_serve():    
    serve = "expert"
    url = requests.get('http://infinite-flight-public-api.cloudapp.net/v1/Flights.aspx?apikey=78879b1d-3ba3-47de-8e50-162f35dc6e04&sessionid=7e5dcd44-1fb5-49cc-bc2c-a9aab1f6a856')
    response = url.json()
    listaDeJogadores = [response[i]["DisplayName"] for i in range(100) if  "IFATC" in response[i]["DisplayName"]]
    return listaDeJogadores


lista = Expert_serve()
print("Lista de jogadores: ", lista)    
print ('Jogadores online: ',len(lista))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.