I'm building a very simple fantasy football web-app using django for a course. One bug I discovered recently was that if a player isn't present in the JSON data that I'm pulling from an api, then it throws an error stating that the local variable for that player's score is being referenced before being assigned. Makes sense given that there is no actual data for that player. So, to fix this, I figured all I needed to do is add an elif (or else) statement to my logic to set that player's score to zero. Here's my code for this so far.
for score in scores:
if score['player_name'] in QBname:
QBscore = float(score['points'])
totalpoints.append(QBscore)
I'm looping through a list called "scores" where I have the player scores coming in from an api call, then I'm comparing the player's name, which is stored in a variable called "QBname", to score['player_name]. Obviously, if score['player_name'] is a match and is in "QBname", I'm then appending that player's score to the variable "totalpoints." Now, to "fix" the problem, and assign a score of 0 to an absent player, I thought I would need something like this...
if score['player_name'] in QBname:
QBscore = float(score['points'])
totalpoints.append(QBscore)
elif score['player_name'] not in QBname:
QBscore = 0
totalpoints.append(QBscore)
However, it just overrides the player's actual score by setting it to zero. I've tested it out by adding the elif line to other players who definitely are present and have a score, but yeah, overrides it. I tried an else statement also which was similar, but same result.
So, this seems like a pretty straightforward problem to fix, but I keep going wrong and would really appreciate another set of eyes on it, so to speak. I'd appreciate any thoughts.
Aucun commentaire:
Enregistrer un commentaire