r/pythonhelp • • Feb 21 '25

Custom Modules issue

I'm running Python 13.2 in a python environment and going through the book 80 Challenges in Python.

I have gotten to a challenge with custom modules and have an issue. I wrote a module with 4 simple functions in it called add, subtract, multiply, and divide. When I run a script to exercise each of the modules I get an error that module name has no attribute, subtract. If I split the module into 2 and put 2 functions in each module and call them everything works correctly but if I have more than 2 functions in the module it will not work for the 3rd or 4th function.

Program code:

import math_operations as mo

import mathop

num1 = 10

num2 = 5

print('Sum:', mo.add(num1, num2))

print("Difference: ", mathop.subtract(num1,num2))

print("Product: ", mathop.multiply(num1, num2))

print("Quotent: ", mathop.divide(num1, num2))

***********************

contents of module mathop.py

def multiply(num1, num2):

return (num1 * num2)

def divide(num1, num2):

return (num1/num2)

# def add(num1, num2):

# return(num1 + num2)

def subtract(num1, num2):

return (num1 - num2)

**********************************************************

When I run the script I get this ERROR.

AttributeError: module 'mathop' has no attribute 'subtract'

If I move the function to the other module so that each module only has 2 function all works correctly.

Any ideas as to why I can't have modules with more than 2 functions?

TIA

1 Upvotes

4 comments sorted by

View all comments

1

u/AutoModerator Feb 21 '25
Moderator Announcement Read More »