 Hello, im trying to use a function inside another function and getting an error:

The function:

``````# Function that recieves number of characters and calculate cost of advertising

if numChar >= 15:
cost = 35.00
else:
cost = 35.00 + ((numChar - 15) * 3.55)

return cost

Function where im using it on:

``````from costAd import *

def main():

costMsg = 0.00
totalCost = 0.00
# apply for loop, with range of 11

for x in range(1,11):
msgLen = input("Enter the length of message" + str(x)+ ": ")

totalCost += costMsg

print "For 10 messages, the cost is: RM %.2f" %(totalCost)

main()``````

The error:

``TypeError: costAd() takes exactly 1 argument (0 given)``

## All 7 Replies

Look at line 11 of costAd(). You called it in your module without an argument. sorry im new to this, how was i supposed to call it? Can you give me some examples please? Thanks. :)

sorry im new to this, how was i supposed to call it? Can you give me some examples please? Thanks. :)

I don't think you need to call it in the module. I'm not too familiar with homemade modules, but just the definition should be enough. Try commenting out line 11 in the module and run it again.

This will allow you to test your module ...

``````# Function that recieves number of characters and calculate cost

if numChar >= 15:
cost = 35.00
else:
cost = 35.00 + ((numChar - 15) * 3.55)

return cost

# this will be ignored if imported as a module
if __name__ == '__main__':

Also notice that 4 spaces is the customary indentation, don't use tabs. Thanks for the help, seems the problem was in the last line of my module

``````# Function that recieves number of characters and calculate cost of advertising

if numChar >= 15:
cost = 35.00
else:
cost = 35.00 + ((numChar - 15) * 3.55)

return cost

No you dont give function an argument. `def costAd(numChar):` Then you most always pass an argument when calling the function.

Your error message is pretty clear.

TypeError: costAd() takes exactly 1 argument (0 given)

Look at vega code. `costAd(20)` If we give a default value as argument we can call like this or with an argument

``````def costAd(numChar=0):
if numChar >= 15:
cost = 35.00
else:
cost = 35.00 + ((numChar - 15) * 3.55)

return cost 