-1

I'm a beginner of Python..! I'm learning how to define a function and how to print out the result. I was going to print out only y value of function. While practicing, I found weird thing...

if I define function as you see and write the code as below:

def f(x):
    y=3*x+5  
    print(y)
print(f(10))

I get the two result as below... :(

35
None

What I want is only the y value. Can you please explain why None printed out?

I'm learning Python little by little. Thank you so much..!!

Jorge Morgado
  • 1,148
  • 7
  • 23

2 Answers2

0

You need to update your codes to return y like this:

def f(x):
    y=3*x+5
    return y
print(f(10))

Since your original codes do not return y, f(10) returns None (the value of y), which leads to your second result being None. The first result from your original codes (which is 35) is from the print function inside your def f(x).

Minh Nguyen
  • 755
  • 5
  • 11
  • Thank you so much!! I was wondering how the results of 35 and None came from, I could easily understand because you kindly explained it to me!! Thank you!! – 배성호 Oct 08 '20 at 05:30
0

You need to return the value of y

def f(x):

    y=3*x+5
    return y

print(f(10))
Priyanka Rao
  • 208
  • 3
  • 11