help using functions from other files - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: help using functions from other files (/thread-14681.html) |
help using functions from other files - lga13 - Dec-11-2018 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 medianto 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 resulthow 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. RE: help using functions from other files - snippsat - Dec-12-2018 (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)
RE: help using functions from other files - lga13 - Dec-12-2018 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)}>') RE: help using functions from other files - snippsat - Dec-12-2018 (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~~~~~~~ |