Is return relevant when you have print?

Hi all,

I’m on the last part of the Function section of learning Python 3 : https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-syntax-cc/exercises/win-percentage

I’m supposed to write a function that calculates the total percentage of games won by a team. Here’s my code :

def win_percentage(wins, losses) :
  percentage = wins/(wins + losses) * 100
  return percentage 

Then I’m supposed to print the percentage using various input values, for ex. : 5 and 5. This is the way I’ve been doing it so far :

print (str (win_percentage (5,5) ) + "%")

It prints 50%.

Considering that I’m supposed to print several results using various input values, I thought it would be quicker to include print within the win_percentage function, so that I would only have to write :

win_percentage(5,5)

and it would come out with 50%.

The code would then look like this :

def win_percentage(wins, losses) :
  percentage = wins/(wins + losses) * 100
  return percentage
  print(str(percentage) + "%")

But when I now call win_percentage(5,5), nothing shows up in the console. However, if I remove the return line so that the code looks like this :

def win_percentage(wins, losses) :
  percentage = wins/(wins + losses) * 100
  print(str(percentage) + "%")   

it does print 50%.

I am just confused with return, I thought it was needed whenever a variable was created within the function but it seems not be the case now. Could someone shed some light ? When would be return needed with print ?

Thanks a lot !

If you have a function that accepts a number and adds 1

Then you could use that function to obtain increasingly higher numbers by feeding the result back into the function.

If you write your result to screen, how do you feed it back into the function to increase it again?

If you started with 0, you could write 1. But you’d never be able to get to 2.

Here. I have a function. (+1). I can combine that function with some other things to end up with a list counting up.

>>> take 10 $ iterate (+1) 0
[0,1,2,3,4,5,6,7,8,9]

You can’t do that with what you suggest. You are suggesting that the result be discarded. It would also be really spammy. I only want the final list, not any of the intermediary results.

1 Like

I would put your print statement right before your return statement, because once your function hits return, it ends.

But it will also work if you unindent your print statement

You actually only need print to clarify things with yourself. Your function does not need print at all.

However, your function often does need return.

Ok, I think I did overfocused on printing things when printing’s probably not the point of most functions. I read somewhere else that things would get clearer as I move on to writing functions that are part of a bigger project. Thank you !

Ok; I didn’t really understood your example because haven’t seen this code before, but I think I have a better grasp on it all now :

def win_percentage(wins, losses) :
  percentage = wins/(wins + losses) * 100
  print(percentage)
  return percentage

win_percentage(5, 5)

will print 50.0 because of the print() that’s within the function;

def win_percentage(wins, losses) :
  percentage = wins/(wins + losses) * 100
  print(percentage)
  return percentage
  
print(win_percentage(5, 5))

will print 50.0 two times : one for the print() within the function and one that shows the returned value;

def win_percentage(wins, losses) :
  percentage = wins/(wins + losses) * 100
  print(percentage)
  
print(win_percentage(5, 5))

will print 50.0 and None because the default value of return is None and I haven’t set it in win_percentage().

So is there a way I can access the returned value other than through print() ? Where is it stored ? Thanks a lot for taking the time !