Help with Python issues

I have a repl where when you start, you select a character, small story and then battle. The issue is where the playerWeaponRange variable shows up as the starting variable and not what it should’ve been set as in the function weaponCalc().

https://replit.com/@jasecrowe/Zombies-haha

playerWeapon = None
playerWeaponRange = None
playerSpecialty = None

def weaponCalc():
  if playerWeapon == "Combat Knife": # PW Starting Weapon
    playerWeaponAttack = 10
    playerCritChance = 12
    playerWeaponCritAdd = 0.2
    playerWeaponRange = 3.58 # In feet : 0.7 + 2.88
  elif playerWeapon == "Pistol": # PO Starting Weapon
    playerWeaponAttack = 50
    playerCritChance = 25
    playerWeaponCritAdd = 2 
    playerWeaponRange = 152.88 # In feet : 150 + 2.88
  elif playerWeapon == "Metal Rebar": # CW Starting Weapon
    playerWeaponAttack = 5
    playerCritChance = 50
    playerWeaponCritAdd = 0
    playerWeaponRange = 7.13 # In feet : 4.25 + 2.88
  elif playerWeapon == "Syringe": # CS Starting Weapon
    playerWeaponAttack = 2
    playerCritChance = 100
    playerWeaponCritAdd = 0.25
    playerWeaponRange = 9.88 # In feet : 7 + 2.88
  elif playerWeapon == "Suitcase": # BM Starting Weapon
    playerWeaponAttack = 5
    playerCritChance = 5
    playerWeaponCritAdd = 1
    playerWeaponRange = 26.88 # In feet, 24 + 2.88

while True:
  clearConsole()
  print("""Hello, survivor. What were you?

1) PRO Wrestler
2) Police Officer
3) Construction Worker
4) Chemical Scientist
5) Bank Manager
""")
  userInput = input()
  if userInput.lower() == "pro wrestler" or userInput == "1":
    clearConsole()
    print("""\033[41mYou were... a Pro Wrestler?\033[0m
    
    You specialize in Melee
    You start with a Combat Knife
    You start with no Armor
    You start with 25 Hitpoints
    You start with 10 Defense
    You start with 5.5 feet/second Speed
    You start with 10 Attack
    You have a 2.5x Crit Multiplier
    
    \033[41mThis is you?\033[0m
    """)
    userInput = input()
    if userInput.lower() == "yes":
      playerSpecialty = "Melee"
      playerWeapon = "Combat Knife"
      playerHitPoints = 25      
      playerDefense = 10
      playerSpeed = 5.5
      playerAttack = 10
      playerCritMulti = 2.5

      print("\033[41mOk then\033[0m.")
      printSlow75(". . .")
      time.sleep(1)
      print()
      printSlow25("\033[41mYou should wake up now, ya'know?\033[0m")
      time.sleep(2)
      break
    elif userInput.lower() == "skip":
      playerSpecialty = "Melee"
      playerWeapon = "Combat Knife"
      playerHitPoints = 25
      playerDefense = 10
      playerSpeed = 5.5
      playerAttack = 10
      playerCritMulti = 2.5
      break
    elif userInput.lower() == "no":
      print("\033[41mThen why lie?\033[0m")
      time.sleep(2)

More underneath not posted

weaponCalc()
armorCalc()

feetFromEachother = 6
clearConsole()
while True:
  weaponCalc()
  armorCalc()
  clearConsole()
  playerTurnGone = "no"
  enemyTurnGone = "yes"
  print(f"""{enemyNarratorDialogue()}
Your Hitpoints : {playerHitPoints}
Your Defense : {playerDefense + playerArmorDefense}
Your Speed : {playerSpeed}
Your Attack : {playerAttack + playerWeaponAttack}
Your Weapon : {playerWeapon}

Distance from opponent in FT : {feetFromEachother}

Enemy Hitpoints : {enemyHitPoints}
Enemy Defense : {enemyDefense}
Enemy Attack : {enemyAttack}

What do you do!

1) Approach {playerSpeed}ft
2) Back away {playerSpeed}ft
3) Attack {playerWeaponRange}ft/{feetFromEachother}ft needed
4) Run away {playerSpeed}/{enemySpeed * 2} needed 
""")

You have variables such as playerWeapon, these are global variables.
Inside of your weaponCalc() function, you have variables with the same names. However, these are only local variables, meaning they will go away when the function ends.
To set values of global variables inside of a function, use the global keyword followed by the variable name, near the start of the function. This will tell python that you are trying to modify the global variable, not setting a new local variable.
Example:

x = None
def change_x():
  global x
  x = 2
change_x()
print(x)

Note having mutable, or changing global variables (not constants) is considered a bad practice in python.
Alternatives include:

  1. Passing the data around as arguments to functions, and returning values from functions (instead of using global variables)
  2. Using one global variable as a collection of values, such as using a dict or custom object.
  3. Passing around a collection of values (such as dict or custom object) to functions.

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