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
#2
(Dec-11-2018, 11:28 PM)lga13 Wrote: how can i make this run so that it will give me the median in main.py?
It should work as long as maths.py and main.py is in same folder.
Have you tried it,do you get error?
Test main.py:
from maths import median

lst = [1, 2, 3, 4, 5]
print(f'Median of list is <{median(lst)}>')
print('-------------')
# Also help() work as you have docstring in function
help(median)
Output:
Median of list is <3> ------------- Help on function median in module maths: median(alist) Calculates the median of a list of numbers.The list must not be empty.
Reply
#3
Thanks for the help that is working as expected!

there was no error message but if i did the likes of print(x,y,z) it wasn't doing anything, can you explain what the f on this line does?

print(f'Median of list is <{median(lst)}>')
Reply
#4
(Dec-12-2018, 12:23 AM)lga13 Wrote: can you explain what the f on this line does?
f-string is the new string formatting(new in 3.6).
format() we have had since 26..
Example.
# older way
>>> first_name = "Eric"
>>> age = 30
>>> print('Name is:{} Age is:{}'.format(first_name, age))
Name is:Eric Age is:30
>>> 
>>> # The new way
>>> first_name = "Eric"
>>> age = 30
>>> print(f'Name is:{first_name} Age is:{age}')
Name is:Eric Age is:30
>>> print(f"Sammy has {4:4} red and {16:16}! blue balloons")
Sammy has    4 red and               16! blue balloons
 
>>> # f-strings support any Python expressions inside the curly braces
>>> name = 'f-string'
>>> print(f"My cool string is called {name.upper()}.")
My cool string is called F-STRING.
 
>>> a, b = 5, 7
>>> f'{a}/{b} = {a/b:.2}'
'5/7 = 0.71'
 
>>> for word in 'f-strings are awesome'.split():
...     print(f'{word.upper():~^20}')
~~~~~F-STRINGS~~~~~~
~~~~~~~~ARE~~~~~~~~~
~~~~~~AWESOME~~~~~~~
Reply


Forum Jump:

User Panel Messages

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