954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie: error using a function

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)
soUPERMan
Light Poster
43 posts since Feb 2010
Reputation Points: 10
Solved Threads: 3
 

Look at line 11 of costAd(). You called it in your module without an argument.

txwooley
Junior Poster in Training
84 posts since Apr 2009
Reputation Points: 10
Solved Threads: 7
 

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

soUPERMan
Light Poster
43 posts since Feb 2010
Reputation Points: 10
Solved Threads: 3
 
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.

txwooley
Junior Poster in Training
84 posts since Apr 2009
Reputation Points: 10
Solved Threads: 7
 

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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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 :)
soUPERMan
Light Poster
43 posts since Feb 2010
Reputation Points: 10
Solved Threads: 3
 
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)
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

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).

soUPERMan
Light Poster
43 posts since Feb 2010
Reputation Points: 10
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: