Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6604107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:06:34+00:00 2026-05-25T19:06:34+00:00

I’m working in python 2.7. I have a list of teams in the following

  • 0

I’m working in python 2.7. I have a list of teams in the following dictionary:

NL = {'Phillies': [662, 476], 'Braves': [610, 550], 'Mets': [656, 687]}

The first value in the list is the amount of runs scored the team has, and the second is the amount of runs that team has given up.

I’m using this code to determine the Pythagorean winning percentage of each team, but I would also like to be able to have the function calculate the total number of runs scored and allowed by the group as a whole.

Right now I’m using:

Pythag(league):
    for team, scores in league.iteritems():
    runs_scored = float(scores[0])
    runs_allowed = float(scores[1])
    win_percentage = (runs_scored**2)/((runs_scored**2)+(runs_allowed**2))
    total_runs_scored = sum(scores[0] for team in league)
    print '%s: %f' % (team, win_percentage)
    print '%s: %f' % ('League Total:', total_runs_scored)

I’m not sure exactly what is going on with the sum function, but instead of getting one value, I’m getting a different value over each iteration of the team and win_percentage, and it’s not the same value…

Ideally, the function would just return one value for the sum of the runs scored for each team in the dictionary.

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:06:34+00:00Added an answer on May 25, 2026 at 7:06 pm

    If you want to have the running total available, or don’t want to iterate over league twice, you can do:

    def Pythag(league):
        total_runs_scored = 0
        for team, scores in league.iteritems():
            # other stuff
            total_runs_scored += scores[0]
            # other stuff
            # runs scored by all teams up to this point
            print 'League Running Total of Runs Scored: %f' % (total_runs_scored,)
        # outside the loop, so total runs scored in the league.
        # will be the same as the last one in the loop
        print 'League Total Runs Scored: %f' % (total_runs_scored,)
    

    Remember that inside the loop you’re talking about a single team, so you don’t need to do a sum to get the runs scored by that team, you instead need to add it to the runs scored by all the previous teams, that is, the scores[0] from the previous iterations of the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.