The counter variable in file hangman.py
line number 36
will not decrease after a wrong attempt.
It goes from 5 to 4 on the first attempt but after that for every wrong attempt it stays the same
Asked
Active
Viewed 63 times
-1

agentsam007
- 3
- 3
1 Answers
0
You've changed your code in that repo and I think you're referring to line 36 in the repo. That being said, python is not pass by reference. When you change counter
inside the function read
, you're not changing the global variable lives
. If you run your program in interactive mode, python -i prac.py
, then run print(lives)
after you can see it's still at it's original value of 5. A work around to this could be to set the global lives
in prac.py as follows:
# original code:
def func(par):
x=par
print(x)
c1.create_text(100,0,fill='white',text=par)
flag_3=ob1.read(x,lives)
# new code:
def func(par):
x=par
print(x)
c1.create_text(100,0,fill='white',text=par)
global lives
flag_3=ob1.read(x,lives)
lives = flag_3
A more elegant solution however would probably be to have the lives
variable as member of your hangman
class.

mTesseracted
- 290
- 1
- 10
-
i tried doing that but there was no change and flag_3 is used to check if the string entered is correct or not so the values of flag_3 will be 0 or 1 – agentsam007 Aug 02 '19 at 05:29
-
It works for me: https://imgur.com/a/2XaWOoS In the version of the code I used (hash 03788ba), your `read` function returns counter, which is simply the input arg counter decremented if the guess was incorrect. – mTesseracted Aug 02 '19 at 16:35
-
As a side note, when trying to post to a forum about a bug, you want to make it a [minimal working example](https://en.wikipedia.org/wiki/Minimal_working_example). This way people don't have to comb through a bunch of extra code that is unrelated to the issue. Most times you'll also find the bug when making the minimal example. – mTesseracted Aug 02 '19 at 16:40