-2

I am trying to make a Ping Pong game using Python and make a scoring system to keep track of how many points each "paddle" or player will score. This is the code I have so far:

score = 0

def point_for_paddle1():
    score = score + 1
    turtle.write(score)

When I run the code it says that the score is not defined. I believe this problem is being caused by the score = score + 1 line.

How should I fix that? I'm fairly new to python but all types of answers are appreciated. Thanks in advance! P.s I'm using Python 3.

Ram
  • 43
  • 7
  • 1
    As a refresher, please read [ask] and title your questions according to the *question you have about the code*, not about the overall problem you are trying to solve. If your question were *actually* "how do I make a score system?" then that would be much too broad for Stack Overflow, as there are any number of design decisions to make (depending on what "score system" actually means to you). When you get an error with your code, a good place to start writing your question title is with the kind of error you got. – Karl Knechtel Dec 03 '21 at 05:18

2 Answers2

1

Python does not include global variables in the function scopes. You have to explicitly mention that you're accessing a global variable inside a function.

In your case, the global variable score is not accessible inside the point_for_paddle function. You can access it using the following way.

def point_for_paddle():
    global score
    score = score + 1
    turtle.write(score)
ThisaruG
  • 3,222
  • 7
  • 38
  • 60
0

Use global to indicate you are using a variable declared globally

score = 0

def point_for_paddle1():
    global score
    score = score + 1
    turtle.write(score)
ksohan
  • 1,165
  • 2
  • 9
  • 23