I'm an intermediate python coder, but i'm still somewhat new to it. I just started using json files (kind of like a database), and my most recent projects was to use the nba database (https://data.nba.net/prod/v1/today.json) for a user input that tells about an nba character that they input in. The problem was that it would pour the entire roster database, so I added a filter. The problem with the filter was that it wouldn't let anything out. If you type Lebron James, shows nothing. Not even an error.
from requests import get
from pprint import PrettyPrinter
BASE_URL = "https://data.nba.net"
ALL_JSON = "/prod/v1/today.json"
printer = PrettyPrinter()
data = get(BASE_URL ALL_JSON).json()
def get_links():
data = get(BASE_URL ALL_JSON).json()
links = data['links']
return links
def ask_player():
players = get_links()['leagueRosterPlayers']
playerselected = get(
BASE_URL players).json()['league']['standard']
'topic' == input('Type the player you wish to know more about:')
playerselected = list(filter(lambda x: x['firstName'] == "topic",playerselected ))
playerselected = list(filter(lambda y: y['lastName'] == "topic",playerselected ))
for standard in playerselected:
name = standard['firstName']
name2 = standard['lastName']
jersey = standard['jersey']
#teams = standard['teams']
college = standard['collegeName']
dob = standard['dateOfBirthUTC']
years = standard['yearsPro']
printer.pprint(f"{name} {name2}, jersey number {jersey}, went to {college} college, was born in {dob}, and has been in the nba for {years} full year(s).")
ask_player()
My code is above. I tried a lot, and I even read most of the database to see if I missed something. Can someone help? It's a project for fun, by the way.
CodePudding user response:
The problem is around these lines of code where topic
should be a variable, not a string. Also, you should assign the input to the topic variable, not using ==
.
The filter compares the first name and last name, so we split the input to two parts.
topic = input('Type the player you wish to know more about:')
firstName = topic.split()[0]
lastName = topic.split()[1]
playerselected = list(filter(lambda x: x['firstName'] == firstName and x['lastName'] == lastName, playerselected ))
The output is like this for Steven Adams
,
('Steven Adams, jersey number 4, went to Pittsburgh college, was born in '
'1993-07-20, and has been in the nba for 8 full year(s).')