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 !