Python Forum

Full Version: How to update score value?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, We're in a beginner Python class and creating text adventure games.
We want to be able to update the score during the game.

We've tried something like this below..and all sorts of other experiments. We simply want to update the global variable of score each time the function is run.
(This current version gives an error)

Thanks for any clues!
score = 0

def showScore():
  score = score + 5
    
showScore()
showScore()
showScore()
You need to have a read of a topic called scope

This is how your code should be formed:

score = 0


def showScore():
    global score
    score += 5

showScore()
Of course, the function is misnamed, because it does not 'show' anything; it simply updates the score with a value of five, each time it is called.
Avoid using global variables in Python, and you should go to great lengths to avoid using the global keyword. You could write your program like this:
def score(change, old_score=0):
    new_score = old_score + change
    print(new_score)
    return new_score

x = score(6)
x = score(1, x)
Output:
6 7
Right now you are thinking "That is ridiculous! The "global" solution in rob101's post is much better."

While it is true that using the "global" keyword results in a cleaner solution for an 8 line program, it does not scale well to an 800 line program or an 800,000 line program. The reasons why have been outlined in many articles online. Search for "global variables are evil". Some of this won't make any sense to a beginning programmer, but they mostly come down to it being difficult to keep track of all the places the variable is modified. This results in programs that are difficult to modify and software bugs that are extremely difficult to track down.
[quote="deanhystad" pid='163835' dateline='1667799368']
Avoid using global variables in Python, and you should go to great lengths to avoid using the global keyword. You could write your program like this:
def score(change, old_score=0):
    new_score = old_score + change
    print(new_score)
    return new_score

x = score(6)
x = score(1, x)
Output:
6 7
Thank you...this certainly works.

I don't quite understand why this part: x = score(6) works though? This is a new concept for me.

in all the tutorials I've seen to run a function...you type the function name...eg. score(6)

What is this "x = " feature called so I can google it and learn how this works?

I thought that just made x into a variable?

Thanks!
I like the way this is written. Breezy, but full of information.

https://python-docs.readthedocs.io/en/la...style.html

Read the whole thing, but the part that answers your question is under the heading "Keyword arguments".
(Nov-07-2022, 08:59 PM)deanhystad Wrote: [ -> ]I like the way this is written. Breezy, but full of information.

https://python-docs.readthedocs.io/en/la...style.html

Read the whole thing, but the part that answers your question is under the heading "Keyword arguments".

Thank you. This gives me some great educational directions...and concepts to learn! (along with other students)

Appreciate it!