0

I'm working on a blackjack game. I put some functions on a different file to help clean up the main file. I put the function that counts the hand value in a separate file in a different function. When I call the variable in the second file, the values change as usual. However, when I call it in the main file, it stays as if nothing happened.

Here's an example:

main.py

import hand

number = ["Ace", "Two", "Three"]
hand = "One"
player_handValue = 0

if playerChoice = "h":
  hand.playerHit(player_handValue)
  print("main.py: " + player_handValue)

hand.py

def playerHit(player_handValue):
  if hand == "Two":
    player_handValue += 2
  print("hand.py: " + str(player_handValue))

This is kind of choppy since it's not the exact thing I wrote but I think it gets the main idea.

Here's the outcome:

hand.py: 2
main.py: 0

As you can see, the 1 from hand.playerHit didn't transfer back to main.py's player_handValue. If anyone could help find a way to fix this, or work around it, please let me know.

What I want:

hand.py: 2
main.py: 2
Amie
  • 31
  • 5
  • 1
    To literally answer your question see [here](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference). Really though, you should just return the new `player_handValue` from the function and use the returned value at the call-site. – Carcigenicate Nov 05 '20 at 16:19
  • In the third statment you loose the reference to the imported module, because you bind the same name to something else. – progmatico Nov 05 '20 at 22:45

0 Answers0