Why does my print statement print "none"?

Hi,

Wondering why the code:

def dog_years(name, age):
print (name + “, you are " + str(age*7) + " years old in dog years.”)

Is returning the result:

Lola, you are 112 years old in dog years.
None
Baby, you are 0 years old in dog years.
None

1 Like

It’s returning none because you are returning a print statement.

But the print is not happening inside the function, but when you call the function.

print(dog_years("Lola", 16))
# dog_years should be replaced by the returned value without print! 

My solution is :

def dog_years(name, age):
  age *= 7
  return name + ", you are " + str(age) + " years old in dog years"
4 Likes

The print statement is executing inside the function, but since it has no return value of its own, the caller sees None coming back (on the return).

Whenever possible, do not print inside a function, but use return and let the caller do the printing, as the above post suggests.

def foo(bar, baz):
    return '{brand} is a very good brand of {product}.'.format(brand=bar, product=baz)

print (foo('Nabob', 'coffee'))    
# Nabob is a very good brand of coffee.

We can do interpolation using format on your return statement…

>>> def dog_years(name, age):
	return '{name}, you are {dog_age} years old in dog years.'.format(name=name, dog_age=age * 7)

>>> print(dog_years("Lola", 16))
Lola, you are 112 years old in dog years.
>>> 
4 Likes

Thank you both for your patient and helpful explanations

1 Like

Please help.
I keep getting a error saying “keyword can’t be an expression”. Why is this code not working?:
def dog_years(name, age):
return “{name} , you are {age} years old in dog years”.format(“name=name”, “age”=age*7)

Careful not to sprinkle quotes all over the place. String literals are for when you want to represent text, if something is code, then it isn’t a textual value and therefore shouldn’t be enclosed in quotes.

You would for example not do this:

"for a in range(3):"
"    print(a)"

Thanks for responding. This is an area I continue to be confused with. While your response is helpful, it is still technical beyond my understanding. Can you please provide several examples of when, when not, quotes are to be used? What are string literals, and what would I compare them to.

Thanks in advance.

The only reason you’d use quotes if you want to represent t-h-e-s-e-p-a-r-t-i-c-u-l-a-r-l-e-t-t-e-r-s as a value.
same as how you’d only use numbers when you want to represent a number.

I had the same problem and came here for help but the explanations found here used techniques not yet learned in the Python 3 course.
I also had the exact same line of code as you and you’ll smack your forehead and call yourself stupid - as I did! - for not finding the solution because it’s so simple.
So I finally caved in and asked for the answer and all you have to do is replace the print function with the return statement…duh!
And it becomes this:

def dog_years(name, age):
  return name+", you are "+str(age*7)+" years old in dog years"