Call a function multiple times; how to add the returned value; Python -




i creating game using python 3.5 exercise.

i have function returns score , want sum score every time functions called in order obtain final score.

the simplified code follows:

letter = 'b'  def func(letter):      score = 0     word='bye'      in word:         if letter == i:              new_word = remove letter word              score += 1             return(new_word, score)         else:             return('try again') 

now imagine function called multiple times in function, how add scores produce final score?

if simplified code seems wrong, main concern how sum numerical return function.

thanks

create new variable totalscore initialized outside function, , have updated each time function called.

letter = 'b' totalscore = 0  def func(letter):      score = 0     word='bye'      in word:         if letter == i:              new_word = remove letter word              score += 1             totalscore += score             return(new_word, score)         else:             return('try again') 

you can access totalscore whenever needed other functions.





wiki

Comments

Popular posts from this blog

elasticsearch - what is the equivalent data type for geo_point in hibernate search? -

firebase - How to wait value in Ionic 2 -

Jenkins: find build number for git commit -