-1

I just started learning to code and I'm currently making a basketball videogame for fun. However, I can't seem to get the score to add up quite right. Every time the loop restarts it just resets to zero then adds 2 to the variable. Is there a way it can keep adding digits each time the loop goes back so it doesn't reset?

Here is a really simple version of it:

score_of_enemy = 0
 
def enemy_score():
    print(" ENEMY SCORE")
    print(score_of_enemy.__add__(1))
    return

enemy_score()   # this is just if the function is ever called
enemy_score()
enemy_score()
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Jwack2021
  • 7
  • 1
  • 1
    Add `score_of_enemy += 1` outside of `print()`, and just `print(score_of_enemy)`, otherwise no values will be added to `score_of_enemy`. – RJ Adriaansen Feb 05 '21 at 22:12
  • Welcome to Stack Overflow. I think [this answer](https://stackoverflow.com/a/3614446) shows an example of what you want. – bad_coder Feb 06 '21 at 00:56
  • 1
    Does this answer your question? [Scoping in Python 'for' loops](https://stackoverflow.com/questions/3611760/scoping-in-python-for-loops) – bad_coder Feb 06 '21 at 00:56
  • 1
    I don't see a loop in your example code. Are the 3 calls to `enemy_score()` simulating the multiple calls that you have in a loop in your full code? – Code-Apprentice Feb 06 '21 at 01:24
  • 1
    I'm curious what example code you are following that shows you to use `__add__()`. As other's noted this is unusual since it's much easier and more natural to write `score_of_enemy + 1` instead. If you see `__add__()` in a code example, I encourage you to avoid that content because it is teaching you terrible habits. Who knows what else they are doing that you shouldn't. – Code-Apprentice Feb 06 '21 at 01:36
  • 1
    Also, this example seems oversimplified from the full code this comes from. To increment a variable, a function is overkill. You can just do it directly with something like `score_of_enemy += 1`. I suspect your actual code is more complex and there is a better solution to your problem. However, I have a hard time recommending one thing because there are many possibilities. – Code-Apprentice Feb 06 '21 at 01:42

2 Answers2

2

For such a simple task, a function doesn't seem to be the right tool for the job. Instead, I would just increment the variable directly:

score = 0

score = score + 1
print(score)

# or
score += 1
print(score)

If you are keeping track of multiple attributes of an "enemy", then you will eventually want to learn about classes. These allow you to store the score along with other data associated with the "enemy".

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Here's the most similar solution to yours that just fixes the issues with your code:

score_of_enemy = 0

def enemy_score():
    global score_of_enemy
    print(" ENEMY SCORE")
    score_of_enemy = score_of_enemy.__add__(1)
    print(score_of_enemy)

enemy_score()  # this is just if the function is ever called
enemy_score()
enemy_score()

Result:

 ENEMY SCORE
1
 ENEMY SCORE
2
 ENEMY SCORE
3

Those issues were:

  1. if you operate on score_of_enemy inside your function without declaring it global, then you won't be modifying the global copy that exists outside the function, which is what you want so that each call to the function adds to the score. So I added the call global score_of_enemy.
  2. score_of_enemy.__add__(1) adds 1 to the value of score_of_enemy but doesn't store the resulting value back in score_of_enemy. You have to do that explicitly, as shown in my version.
  3. You can't put an assignment statement inside a print statement, so I broke the two operations onto separate lines.
  4. You don't need the return statement if you don't give it an argument to specify something to return. Just return at the end of a function is redundant.

Using __add__ is unusual, as it's the same thing as +. Most programmers use +. You can abbreviate that line further using +=. So the equivalent of the score_of_enemy = score_of_enemy.__add__(1) is score_of_enemy += 1.

To give you an idea of how to avoid the use of global (per the comments), here's another version that avoids its use. It seems more complicated here, and it is, but that's only because this code is just a simple example. In real life, following this pattern will make your code more readable and stable:

score_of_enemy = 0

def enemy_score(score):
    print(" ENEMY SCORE")
    score += 1
    print(score_of_enemy)
    return score

score_of_enemy = enemy_score(score_of_enemy)  # this is just if the function is ever called
score_of_enemy = enemy_score(score_of_enemy)
score_of_enemy = enemy_score(score_of_enemy)
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • 2
    As a side note for the OP, using a global variable in this way is considered a bad practice. In large programs, global variables can lead to bugs that are difficult to find. I encourage you (the OP) to learn more about local variables, function parameters, returning a value from a function, and classes with methods and fields. These are the tools that we use to encapsulate data and behavior that pretty much render global variables unnecessary. I know there's a lot to learn. I just want to nudge you in the right direction. – Code-Apprentice Feb 06 '21 at 01:28
  • 1
    I concur. I just wanted to limit the feedback to the most direct issues. I've got a lot of Python code in my world, some that I wrote myself, and other that I maintain. I wouldn't be too surprised if I were unable to find a single use of the `global` keyword in all that ode. It's just not done. Python, IMO, would be a better language with that keyword removed from its syntax. – CryptoFool Feb 06 '21 at 01:31
  • 1
    To be clear, my comment is for the OP and future readers. The trouble with avoiding the `global` keyword in an answer such as this is that there are practicially infinite possible solutions. The "right" one is not immediately obvious from the limited information from the quesiton. So I understand why you wrote your answer in this way. – Code-Apprentice Feb 06 '21 at 01:34
  • 1
    I agree with @Code-Apprentice using a [magic method](https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types) on a variable isn't the best way of doing things here. – bad_coder Feb 06 '21 at 01:36