0

I am trying to make a module but not sure what exactly they are. I will post what the requirements of it are.

REQUIREMENTS Make a module called 'funct' that holds the following information -

  1. def total(val) - returns sum of all # in the list 'val'.

  2. def sub(val) - returns the difference of all # in the list 'val'.

  3. def times(val) - returns the product of all # in the list 'val'.

  4. def div(val) - returns the result of dividing all # in the list 'val'. If first # is 0, then the result is 0. If any other # are 0, use sys.exit to stop it & display 'invalid list.'

Here is what I'm thinking it is supposed to be like:

import funct

funct.total([1, 2, 3, 4])
funct.sub([1, 2, 3, 4])
funct.times([1, 2, 3, 4])
funct.div([1, 2, 3, 4])
  • 1
    How can you not know what they are? Surely whoever gave you this assignment taught the basics first. Were you absent for that class? – Barmar Feb 06 '23 at 21:42
  • Does this answer your question? [how to make a python module or function and use it while writing other programs?](https://stackoverflow.com/questions/39978375/how-to-make-a-python-module-or-function-and-use-it-while-writing-other-programs) – Gino Mempin Feb 06 '23 at 22:30
  • Or the more canonical [How to write a Python module/package?](https://stackoverflow.com/q/15746675/2745495) – Gino Mempin Feb 06 '23 at 22:32

2 Answers2

0

A module in python is a file containing code, i.e. you need to create a file called funct.py in the same folder as your main.py (or whatever you called the example above).

Then you need to implement each of the required functions, i.e. you code should look like

def total(val):
    pass

def sub(val):
    pass

def times(val):
    pass

def div(val):
    pass

Then in place of pass you need to code up the required logic

David Waterworth
  • 2,214
  • 1
  • 21
  • 41
  • Ok so when the module 'funct.py' is run, it is not supposed to give any results? And could you by chance help out with any of the logic given the requirements? –  Feb 07 '23 at 16:38
0

A module is nothing else like a separate python file. So you can just create a file funct.py and define all the needed functions, so:

funct.py:

def total(lst : list) -> int:
    acc = 0
    for i in lst:
        acc += i
    return acc  # return the accumulated sum

def sub(...):
    pass

def times(...):
    pass

def div(...):
    pass

Then in another python file, but in the same folder, create the main.py file or any other filename which imports this module and uses the defined functions:

main.py

import funct

funct.total([1, 2, 3, 4])
funct.sub([1, 2, 3, 4])
funct.times([1, 2, 3, 4])
funct.div([1, 2, 3, 4])

Here you can see some further reading and informations about python modules.

kaiserm99
  • 146
  • 8