I've been trying to figure this out for a few days, and my teacher can't really answer my question, as he is just as stumped as I am.
Python is supposed to be pass by reference, but I keep getting pass by value when it comes to returning a bool variable from a function, or even changing the status of a bool variable in an if statement in main
I have tried to apply many different solutions trying to get a bool variable to change but no matter what I do it never changes. Is there a reason for this, or something I'm not catching onto? I know with C++ you have to use & to pass by reference anything in a function, but since Python is supposedly pass by reference as default, I know of no operators to force the variable into a reference variable.
I have tried 3 approaches to this solution:
The first being to pass the bool variable into a function and test to see its status, and depending on the nature of the beast, it may or may not change, and if the status of the variable changes, it's done inside the function, and then the variable is returned.
The second approach is just assigning the bool variable a value depending on the outcome of the nature of the beast in an if statement in main
The third approach I've done is just to return the value of the variable (True or False), and in main() I assign the bool variable to that function, so theoretically the bool variable would catch the value of the bool function.
None of these works
I ran into this same exact problem when trying to make a tic-tac-toe game in python a few weeks ago - it would only pass by value...
I'm stumped, and if I can't get this to work, I have to down play this text game and make it more automated, but I'd rather see it as interactive as possible. If someone can point me in the right direction it'd be much appreciated.
This is my code:
1st Approach - returning a value or bool variable from a function
result = checkAnswer("C", userAnswer)
if(result):
#if answer is incorrect, display message as such
wrongAnswer()
#choose what limb to cut off teacher
choice = chooseLimb()
#check to make sure limb isn't already hacked off
checkLimb(armRight)
if(checkLimb(armRight)):
print "That limb has already been hacked off!"
#test new value of armRight
print "Value of armRight is now",armRight
Note ** when running this code (and the rest I hadn't posted because it's irrelevant to this question) I purposely choose the wrong answer, and choose a limb to cut off to force the value to change
the new value (last line of above code) doesn't change value
calling the function checkLimb(limb) is as follows:
# CHECK LIMB #
def checkLimb(limb):
if limb == True:
limb = False
return limb # or just returning false - same outcome - no change
else:
return True
2nd Approach: - assigning the bool variable to bool function
if(result):
#display wrong answer message
wrongAnswer()
#choose what limb teacher loses
choice = chooseLimb()
result = checkLimb(armRight)
#test value of bool
print "Value of armRight is ",armRight
the value of the bool stays true again
I have tried assigning the arguement passed into the function (arguement of bool value, of course) and returning the arguement, as well as returning False instead of the arguement, and the value stays true
3rd Approach: - assigning value to bool variable in if statement in main()
#test for right answer to a question
result = checkAnswer("C", userAnswer)
if(result):
#display wrong answer message
wrongAnswer()
#choose what limb to lose
choice = chooseLimb()
if choice == 1 and armRight == True:
armRight = False
print "The teacher has lost his right arm"
print "Value of armRight is ",armRight
Yet the value of armRight stays true
The armRight variable is declared and assigned to True before it is called for anything.
armRight = True
It never changes ><
Any ideas?