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

  • SEARCH
  • Home
  • 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 6615361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:27:09+00:00 2026-05-25T20:27:09+00:00

Here is part of my module: gm.py def avg_list(list): sum = 0 for num

  • 0

Here is part of my module: gm.py

def avg_list(list): 
    sum = 0
    for num in list:
        sum += num

    avg = float(sum)/len(list)
    print avg



def median(list):
    i = len(list)
    if not i%2: # if i divided my 2 has no remainder

        return (list[(i/2)-1]+list[i/2])/2.0 # return the value of this block


    else:
        median = sorted(list)[len(list)/2] # otherwise, when the list is sorted, the index of len(s) / 2 is the middle number.
        return median

When I save this as ‘gm.py’ and open a new script page to input the following function:

import gm
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = median(list) # same for median
    return stats

When I run this program and type stats([2,3,4,5,6])… I get an error saying global variable avg_list not defined. I’m not sure if I’m doing the import correctly?
Do I need to do something like… from gm import avg_list() ?

  • 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-25T20:27:09+00:00Added an answer on May 25, 2026 at 8:27 pm

    Either reference the functions on the module object:

    import gm
    def stats(list):
    
        stats = {}  # empty dict named stats
        stats['average'] = gm.avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
        stats['median'] = gm.median(list) # same for median
        return stats
    

    or import the functions directly into the global namespace:

    from gm import avg_list, median
    def stats(list):
    
        stats = {}  # empty dict named stats
        stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
        stats['median'] = median(list) # same for median
        return stats
    

    Note that you should not name a variable list. This masks the built-in list() function / type and can cause confusing errors later if you need to use it.

    You can write

    stats = {}  # empty dict named stats
    stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = median(list) # same for median
    return stats
    

    as

    stats = {'average': avg_list(list), 'median': median(list)}
    return stats # or just return the dict literal, no real need to give it a name.
    

    I think you should look at the first branch of your median function. Does the list need to be sorted there too, like in the second branch?

    Your avg_list function is also masking a built-in function, sum(), which you could use here instead of manually adding:

    def avg_list(list): 
        avg = float(sum(list))/len(list)
        print avg
    

    Finally, look at the last line of that function — it’s printing the avg, but stats is expecting it to return the avg. The two aren’t the same.

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

Sidebar

Related Questions

here is the part if having error. Fatal error: Using $this when not in
Here's part of my Note class: class Note attr_accessor :semitones, :letter, :accidental def initialize(semitones,
Here's part of my Django app's models.py : class Person(models.Model): birth_year = WideYear(null=True, blank=True)
Here is part 1 of our problem (Loading a dynamically generated XML file as
Here is part of my web.xml: <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:application-config.xml </param-value> </context-param> application-config.xml uses
Here's part of my code for a jQuery word counter. My question is: how
EDIT Sorry I forgot the most important part here. Each key can have more
I'm using implementation of SimpleCursorAdapter , which implements SectionIndexer . Here's part of it:
Here is a part of my html code (video urls marked with a django-template
So two part question here. Basically, what is the proper practise for javascript function

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.