Variable not adding in Python

Problem description:
I am trying to debug some codes I wrote for one Python class, have been struggling for a while:
not sure why current_score doesn’t add 1 every time input answer is right

Expected behavior:
current_score +1 works

Actual behavior:
shows 0 for every iteration

Steps to reproduce:

Bug appears at this link:
https://replit.com/@RuoxiChen/higher-lower-start#main.py

Browser/OS/Device:

That seems like an error with your code and not replit

2 Likes

I’m not sure why current_score isn’t going up, but I do know that you have another problem. When the user chooses incorrectly, game_stop = True, but game_stop was not defined before. So you should first define game_stop at the top of your code as False and use global game_stop before you change the value of it. That should fix the problem when the user chooses incorrectly.

1 Like

In line 49, you called the game(current_score) without anything to store the return value, while in the game() function you are having a local variable(argument) which is named current_score, same as the global one you are getting.

In python, variable intercepting are being checked in order, the first to be checked is local, and the third is global, which means if you do anything to current_score in game function it only change the local one.

After all, you decided to return the current_score, which is a good choice since you were not doing anything to the global one, however in line 49 when you called it, you didn’t catch the return value, which goes back, it doesn’t change the global but local one. Since the global one is not being changed, it doesn’t add up into the global one.

Solution: change line 49 to
current_score = game(current_score)

alternative solution: before line 35 add
global current_score

4 Likes

Also, I noticed you only used game() once, in line 49.
Therefore there is another alternative solution.

You can remove the returns in the game function and move the entire thing to replace the game() calling function in line 49

The advantage is that you don’t need to worry about local and global variables messing around, but it also make your code a little bit more messy

3 Likes

(Yep it’s me again)
DISCLAMER, THIS IS VERY LONG, PLEASE DON’T BE INPATIENT READING THIS EVEN ITS HARD NOT TO

Well I can see a few more problem other than the current_score not adding up

Until now, I saw a few problem is in your code.
First one, I can see you added a few things to game() but not fixing the current_score problem, you can fix that in many ways, 3 of them I have replied earlier

Second one, I can see you added more arguments to the game function, which is unnecessary for me, and it produced more problems than it before having more arguments:

First, you added argument chosen_two, this is being declared global in line7, as it is living directly in main’s directory. Declaring it within function or so, in this case is argument in function, makes everything inside to be local, not affecting the one declared in line7. For now it doesn’t cause trouble but it is a risk factor for the program to fail, big one.

Second is the game_stop argument, like why? You declared it as global, in line 44, fixed the problem that the game won’t stop as @QwertyQwerty88 mentioned earlier, but you made it local, again, in the game function, which make the code in line 44 nearly useless, going straight to the point: Everything is done with the argument game_stop but not the global game_stop used for ending the game.

Third one, your choose_new(chosen_two) in line 36 calling to line 25 confused me. There is a same mistake as what caused the problem you opened this question for, but this time it doesn’t fail, why?
It is because you are calling it inside the variable, with the same variable. Confusingly but thankfully, it caused the chosen_two variable to change, just like what the choose_twoh) function is meant to do, just in a complete different way by editing it as enclosing scope.
In short, task failed successfully.

Fourth one, if this is designed on purpose ignore this, but for choose_new function in line 25 to 31, you replaced the versus with something new, but only changed the second one. Which in short, the first one will always be the same unless you restart the entire program.

These are all problem or risk I found and feedback in a over complicated way, I hope this can help making your program work as intended

3 Likes

current_score +1 will never work in Python. It seems like you already got that fixed though as I see your code now says current_score += 1. Alternatively, you could use the inefficient method current_score = current_score + 1, but I see no reason to do that. So, it seems to me that you could mark this topic as solved.
On a side note, you need a clear() command. See this one I got from KanavGupta7:

# To use, type "clear()"
clear = lambda: print("\033[H\033[2J", end="", flush=True)

See more useful functions in my Python library.

3 Likes

this also works :smiley: clear = lambda: print("\033c", end="", flush=True)

3 Likes

@bigminiboss, I’ve missed you. How are things going for you? (kinda off-topic but general question)

2 Likes

Thank you! In my Tiny Number Guessing Game, which I recieved some community help on, we ended up using print("\033c"), but that took me to the second line, so for everything else I kept using the other one. Now I will update it to this as it does work and is shorter.
EDIT: I got it updated.

3 Likes

thanks, yeah I’ve done some testing (too lazy to get it here) but it’s also sometimes slightly faster?

thanks :smiley: yeah I’m doing ok just some stuff hit me like a truck :frowning:

Hi @TaokyleYT , really appreciate your help. I solved all the problems following your instruction (need to read more about global and local scope :slight_smile: ), except one error :
Traceback (most recent call last):
File “main.py”, line 53, in
chosen_two = choose_new(chosen_two)
File “main.py”, line 28, in choose_new
data.remove(dic1)
ValueError: list.remove(x): x not in list

Well it’s because 1 important thing missing in function choose_new(), can you see it?

You extracted the 2 that was used to versus, and deleted it from data, which is good for not overlapping questions. Then you made one for the new question, replaced it with the second one. Lastly, return the chosen_two

Well, you deleted the first versus item from the list but not the second one, and you didn’t do anything to the first versus item but the second one, see the problem?

That’s why the second time it shows error, the item is already being removed for the first round, it cannot find the item that does not exist anymore, that’s why it raised error.

This time no solution, try to fix this since this is pretty straight forward to solve. Feel free to PM me if you got any more problem (so the main page of the replit ask won’t always have us and being annoyed etc).

2 Likes

Thanks a lot :slight_smile: I thought as long as I don’t return “data”, then data will go back to its original assigned values because it’s global var. :sweat_smile:

Hi @RuoxiChen thanks for your post.

This isn’t a bug encountered while using Replit so I moved it to code help.

Hopefully someone from the community is able to have a look at your code and suggest some ideas, however please note that the 100 Days of Coding course you are working through isn’t linked to Replit.

2 Likes

current_score + 1 will never work in Python, but you seem to have that fixed already by using current_score += 1. The problem you do still have is that the program does nothing with the input it gathers from the user. I fixed the program here. Now it adds to your score if you’re right, and you lose if you’re wrong. I added .lower() to guess so that whether the user puts in a capital letter or a lowercase letter Python will take it as a lowercase. I also removed the extra “o” in “loose”.

Basically I added the user’s guess as a parameter for the game function. Overall the code required only a small tweak to make it work.

3 Likes

I applaud you for taking time to review code, but you should probably clean it up so it looks less condescending

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.