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 

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

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)+ ": ")
		costMsg = costAd(msgLen)
		
		totalCost += costMsg
	
	print "For 10 messages, the cost is: RM %.2f" %(totalCost)
	
main()

The error:

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

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 
# of advertising, save module as costAd.py (case sensitive)

def costAd(numChar):
    if numChar >= 15:
        cost = 35.00
    else:
        cost = 35.00 + ((numChar - 15) * 3.55)
        
    return cost
    
# test module costAd
# this will be ignored if imported as a module
if __name__ == '__main__':
    print costAd(20)
    print costAd(10)

Your main program is okay.
Also notice that 4 spaces is the customary indentation, don't use tabs.

Edited 6 Years Ago by vegaseat: imported

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 

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

costAd() <-- dead code after the return statement :)

costAd() <-- dead code after the return statement :)

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

print costAd()
print costAd(20)

Edited 6 Years Ago by snippsat: n/a

Yeah, i never meant to use it on the same code, it was meant to be imported then the argument passed on another function (main function).

This question has already been answered. Start a new discussion instead.