0
async def purchase(ctx, weapon, quantity):
    if weapon == "0":
        weapon_name = "soldier#"
    elif weapon == "1":
        weapon_name = "sniper#"
    elif weapon == "2":
        weapon_name = "demolitionist#"
    elif weapon == "3":
        weapon_name = "spy#"
    elif weapon == "4":
        weapon_name = "armored_vehicles#"
    elif weapon == "5":
        weapon_name = "tank#"
    elif weapon == "6":
        weapon_name = "transport_truck#"
    elif profileData["rank"] >= "30":
        if weapon == "7":
            weapon_name = "nuclear_missile#"
        elif profileData["rank"] >= "50":
            if weapon == "8":
                weapon_name = "thermonuclear_missile#"

So basically if the weapon id they input for the argument it equals as a string like soldier# which affects how many soldiers they have. I just want to know how to convert it to a global var

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Novial
  • 44
  • 1
  • 7
  • yea i think it is: async def purchase(ctx, weapon, quantity): – Novial May 20 '18 at 15:18
  • 1
    Possible duplicate of [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – ᴀʀᴍᴀɴ May 20 '18 at 15:19
  • heres my code: https://hastebin.com/uqobegaxuq.py after i put the global var in and did %purchase 0 1, it didnt do anything – Novial May 20 '18 at 15:25
  • You cannot *"convert a local variable to a global variable"*. Furthermore, you shouldn't use global variables, especially if you are learning Python. You will just learn a bad practice. What is it actually that you want to achieve here? – zvone May 20 '18 at 17:00
  • if weapon = "0" then it'll create a new var that is weapon_name like soldier#, then it'll grab information from a json file.. – Novial May 20 '18 at 18:03

1 Answers1

1

you must declare the variable before use it in if Statements

async def purchase(ctx, weapon, quantity):
    weapon_name = ""  # <= just add this ligne
    if weapon == "0":
        weapon_name = "soldier#"
    elif weapon == "1":
        weapon_name = "sniper#"
    elif weapon == "2":
        weapon_name = "demolitionist#"
    elif weapon == "3":
        weapon_name = "spy#"
    elif weapon == "4":
        weapon_name = "armored_vehicles#"
    elif weapon == "5":
        weapon_name = "tank#"
    elif weapon == "6":
        weapon_name = "transport_truck#"
    elif profileData["rank"] >= "30":
        if weapon == "7":
            weapon_name = "nuclear_missile#"
        elif profileData["rank"] >= "50":
            if weapon == "8":
                weapon_name = "thermonuclear_missile#"
Sakhri Houssem
  • 975
  • 2
  • 16
  • 32