Python Forum
How to update score value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to update score value?
#1
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()
Yoriz write Nov-07-2022, 06:25 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Mavoz likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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.
Reply
#4
[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!
Reply
#5
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".
Mavoz likes this post
Reply
#6
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Average score MartinEvtimov 5 6,854 Apr-02-2017, 07:35 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020