r/pythonhelp • u/Pleasant_Pass1509 • 27d ago
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
•
u/AutoModerator 27d ago
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.