0
def cent_to_fahr(cent):
    print (cent / 5.0 * 9 + 32)

print (cent_to_fahr(20))

The output is this :

68.0
None

Why this output has None?

I didn't get this result when I use just cent_to_fahr(20). Can I ask why it happens ?

3
  • cent_to_fahr prints the result and the caller prints the return value of this function which is None. Commented Oct 24, 2016 at 18:13
  • I didn't get this result when i use just cent_to_fahr(20).Can i ask why it happens ? Commented Oct 24, 2016 at 18:26
  • In that case you have only the print in the function which prints the calculated value. Commented Oct 24, 2016 at 18:31

2 Answers 2

2

To put it into context, let's try understand what causes each line of output you received:

  • 68.0 is printed thanks to the contents of your function. This could be seen as the "end" of that computation's "life", that value/result is no longer available for further computations.
  • None is what the function returns, and in this case is also quite useless.

Now that we understand that better, I would recommend adjusting the function to return the value computed.

def cent_to_fahr(cent):
    return (cent / 5.0 * 9 + 32)

That way when the function is called (in context of further functions) it will return a value that that can be further processed (in this case with print()):

>>>print(cent_to_fahr(20))

Which will print 68.0.

Sign up to request clarification or add additional context in comments.

Comments

2
def cent_to_fahr(cent):
    return (cent / 5.0 * 9 + 32)

print (cent_to_fahr(20))

a function needs to return a value to have an output other then None

2 Comments

I didn't get this result when i use just cent_to_fahr(20).Can i ask why it happens ?
@Gingerbread think of return like just a number. If you call Cent_to_fahr(20) you get 68.0 if you say a = Cent_to_fahr(20). a is now 68.0. You need to print the number to see it. so print(Cent_to_fahr(20)) will print it but just doing Cent_to_fahr(20) will do nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.