Python TypeError: %d format: a number is required, not NoneType
function, python
Solution
You should be returning the results from your functions, not printing them:
def substract (a,b):
print "Substract %d from %d." %(a,b)
return a-b
def multiply (a,b):
print "Multiply %d*%d." %(a,b)
return a*b
def divide (a,b):
print "Dividing %d and %d." %(a,b)
return a/b
Without a return statement, Python functions always return the default value of `None`. This means that when you get to here:
height = substract (78,4)
weight = multiply (90,2)
iq = divide (100,2)
`height`, `weight`, and `iq` will all be assigned to `None`, which causes the string formatting operation to fail with your error.
Problem
I am newbee trying to learn python using LPTH. I trying to do exercise 21 in a different way. Instead of putting the values in the variable, I am asking the user to put the value. ``` def add(a,b): print "Add %d and %d." %(a,b) print a+b def substract (a,b): print "Substract %d from %d." %(a,b) print a-b def multiply (a,b): print "Multiply %d*%d." %(a,b) print a*b def divide (a,b): print "Dividing %d and %d." %(a,b) print a/b total_age_of_the_class = add(int(raw_input(">")) , int(raw_input(">"))) #height_difference = substract (int(raw_input("Ram's height:"), int(raw_input("Shyam's height:")))) height = substract (78,4) weight = multiply (90,2) iq = divide (100,2) print "Age:%d, height:%d, weight:%d, iq:%d" %(total_age_of_the_class, height, weight, iq) ``` For some reason it throws an error when the code run the last line of the code. Thanks in advance for your help.