So, I'm teaching myself Python through instructional stuff I find online and through googling (mostly googling actually). Whenever I learn something new, I try to write my own little script implementing it, as opposed to just copying what I found and moving on to the next subject.
Now, I just learned about functions and modules, how to import them and how to call them up (not sure if my lingo is correct here).
So, I wrote this script called math.py
Code:
def run(int1, int2):
add = int1 + int2
sub = int1 - int2
mul = int1 * int2
div = int1 / int2
print "The sum of %d and %d is:" % (int1, int2), add
print "%d subtracted from %d is:" % (int2, int1), sub
print "%d times %d is:" % (int1, int2), mul
print "%d divided by %d is:" % (int1, int2), div
It works just fine on its own, that is, if I add another line in the end like so:
and then run the program, the output looks as intended:
Quote:
The sum of 6 and 3 is: 9
3 subtracted from 6 is: 3
6 times 3 is: 18
6 divided by 3 is: 2
However, if I make another script, callMath.py
Code:
import math
math.run(6, 3)
I get the following error message:
Quote:
Traceback (most recent call last):
File "callMath.py", line 3, in <module>
math.run(6, 3)
AttributeError: 'module' object has no attribute 'run'
-----------------------------------------------------
I've written and imported other scripts like that before. This was basically just a repetition exercise of what I had learned yesterday, before I move on to today's lesson.
These are two scripts I have written yesterday:
cmpr.py
Code:
def compare(int1, int2):
if int1 > int2:
print "%d is greater than %d." % (int1, int2)
elif int1 < int2:
print "%d is smaller than %d." % (int1, int2)
else:
print "%d and %d are equal." % (int1, int2)
callCmpr.py
Code:
import cmpr
cmpr.compare(5, 3)
If I run callCmpr.py, the output is a satisfactory:
As far as I'm able to tell, I'm doing the exact same thing in both cases. Googling didn't help this time, either.
Last edited by Sugar Nut; 12-14-2014 at 05:57 AM.