Reputation: 2147
I'm trying to get a list like this, where the two collected numbers are sum together and deliver the final result of that sum team1Goals + team2Goals
:
FT 4 + 1 - Atletico El Vigia v Llaneros de Guanare
Postp. 1 + 2 - Deportivo Lara B v Atletico El Vigia
Postp. 0 + 0 - Llaneros de Guanare v Urena SC
FT 1 + 0 - Rayo Zuliano v Universidad de Los Andes FC
Expected Result:
FT 5 - Atletico El Vigia v Llaneros de Guanare
Postp. 3 - Deportivo Lara B v Atletico El Vigia
Postp. 0 - Llaneros de Guanare v Urena SC
FT 1 - Rayo Zuliano v Universidad de Los Andes FC
I tried using the sum()
method, but it gives me an error and even giving me a hint that I honestly don't know how to go about using:
TypeError: sum() can't sum strings [use ''.join(seq) instead]
response = requests.get(url, headers=headers).json()
stages = response['Stages']
for stage in stages:
events = stage['Events']
for event in events:
outcome = event['Eps']
team1Name = event['T1'][0]['Nm']
if 'Tr1OR' in event.keys():
team1Goals = event['Tr1OR']
else:
team1Goals = '0'
team2Name = event['T2'][0]['Nm']
if 'Tr2OR' in event.keys():
team2Goals = event['Tr2OR']
else:
team2Goals = '0'
print('%s\t%s - %s v %s' %(outcome, [sum(team1Goals,team2Goals)], team1Name, team2Name))
Upvotes: 0
Views: 94
Reputation: 11
I am not sure if I got your question correctly, but (now) as I understand is that we can change the STR to INT by doing the following:
print('%s\t%s - %s v %s' % (outcome, sum([int(team1Goals),int(team2Goals)]), team1Name, team2Name))
Upvotes: 1
Reputation: 339
Type cast the string variables to integers:
print('%s\t%s - %s v %s' % (outcome, sum([int(team1Goals),int(team2Goals)]), team1Name, team2Name))
Upvotes: 1
Reputation: 325
So, The sum is only for integers, adding integers, That's what the error says, and has the solution too
Use: variable_name = str(team1Goals) + str(team2Goals)
Upvotes: 1
Reputation: 177
Sum can add up all values in a array given that they are of the same type. You can only use float and integers, while you have used strings.
Upvotes: 1