r/programminghelp • • Feb 25 '21

Python Python Def function help

I'm a relative Python noob, I'm just wondering how I would get the values on the right of the image to increment or decrease depending on weather the number is even instead of the value displayed being -5 or 10. What I hope to be able to do is have the values on the right add up or decrease depending on weather the value produced by the roll is even.

Here is the image: https://imgur.com/ROhw8wU

4 Upvotes

5 comments sorted by

View all comments

2

u/marko312 Feb 25 '21

This is a problem of local variables - essentially, the playerscore in the function is a copy which gets destroyed after the function ends.

You could return playerscores and assign the returned value to player1s.

1

u/[deleted] Feb 25 '21 edited Feb 25 '21

Thanks!, how might that be implemented into the above program? I'm not that familiar with the return function.

2

u/marko312 Feb 25 '21

A return is used to get values back from the function. Inside the function (after everything else):

return playerscore

and where it is called

player1s = process(player1s)

so essentially this becomes

process(player1s)
player1s = playerscore # playerscore from the function

(but playerscore isn't actually accessible).

2

u/[deleted] Feb 25 '21

Thanks a lot mate, you've been a great help!