0

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))
1
  • Elaborate your question on What you are getting? and What you want? Commented Jul 26, 2021 at 3:32

4 Answers 4

1

Type cast the string variables to integers:

print('%s\t%s - %s v %s' % (outcome, sum([int(team1Goals),int(team2Goals)]), team1Name, team2Name))
Sign up to request clarification or add additional context in comments.

1 Comment

Please edit your answear to complete model, like this: sum([int(team1Goals),int(team2Goals)]) or print('%s\t%s - %s v %s' % (outcome, sum([int(team1Goals),int(team2Goals)]), team1Name, team2Name)), thanks a lot for support! Perhaps older users may complain about your simple answer, I've seen it happen, so to lessen the chance of you losing points in the answer, I recommend doing this.
1

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.

2 Comments

Hi mate, would you have any way to add the answer on how I could modify my code so that the result I'm looking for is achieved? Thanks in advance!
It seems that you need to add the numbers and not concatenate, so first convert them to integers using the int() function.
1

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)

2 Comments

Hi mate, I tried to use it like this, but the result was another error: TypeError: str.join() takes exactly one argument (2 given)
The .join() function is used, to connect a list or other iterable element using a provided string. if you want to use the .join() function, you'd have to use it like this: ", ".join(goals), while goals could be a list. Read here for more reference: w3schools.com/python/ref_string_join.asp
1

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))

1 Comment

Hello mate, let's assume that I have team1Goals as 1 and team2Goals as 5, the result I expect is 6, but in your indication the achieved result is 5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.