Python Forum
help using functions from other files
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help using functions from other files
#1
Can anyone assist with this, I am trying to import a calculation from maths.py into my file main.py, I think i have it set properly to pull the calculation through by using

 from maths import median
to get the median function to come over to main.py

I need to find the median of a list of numbers and have it print out the median.

this is my code for the calculating the median

def median(alist):
    """ Calculates the median of a list of numbers.The list must not be empty."""

    number_of_values = len(alist)
    sorted_list = sorted(alist)

    # Two cases, depending on whether the number of values is odd or even.
    quotient = number_of_values // 2
    remainder = number_of_values % 2
 
    if (remainder == 1):
        result = sorted_list[quotient]
    else:
        result = (sorted_list[quotient - 1] + sorted_list[quotient]) / 2
    return result
how can i make this run so that it will give me the median in main.py?

any help and advice would be great - I'm using 3.7.0 if that helps at all.
Reply


Messages In This Thread
help using functions from other files - by lga13 - Dec-11-2018, 11:28 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020