'Python local variable referenced before as assignment

So basically i defined the variable before (globally) However, it still says variable referenced before assignment.

I am a beginner and i dont know how to use this so please forgive me if i am doing something wrong

import time
import multiprocessing
i = True
upgrade_level = 0
money = 10

def clear_chat():
  num = 0
  while num < 100:
    print(" ")
    num += 1




def loop1():
  while i:
    question = input("Would you like to upgrade? (y)\n")
    if(question.lower() == "y"):
      if(money >= 10):
        clear_chat()
        money = money-10
        print("You upgraded once")
        print("Your upgrade level is " + str(upgrade_level))
      elif(money < 10):
        clear_chat()
        print("You do not have enough money ($10) to buy an upgrade")
    else:
      clear_chat()
      print("You can only input (y)")
  
def loop2():
  while i:
    money += 10
    time.wait(10)

p1 = multiprocessing.Process(target = loop1())
p2 = multiprocessing.Process(target = loop2())



Solution 1:[1]

import time
import multiprocessing
i = True
upgrade_level = 0
money = 10

def clear_chat():
  num = 0
  while num < 100:
    print(" ")
    num += 1




def loop1():
  global money
  while i:
    question = input("Would you like to upgrade? (y)\n")
    if(question.lower() == "y"):
      if(money >= 10):
        clear_chat()
        money = money-10
        print("You upgraded once")
        print("Your upgrade level is " + str(upgrade_level))
      elif(money < 10):
        clear_chat()
        print("You do not have enough money ($10) to buy an upgrade")
    else:
      clear_chat()
      print("You can only input (y)")
  
def loop2():
  global money
  while i:
    money += 10
    time.wait(10)

p1 = multiprocessing.Process(target = loop1())
p2 = multiprocessing.Process(target = loop2())

I included global keyword below loop1 func declaration

Solution 2:[2]

You need to add the global keyword in each function where you want to use it to access it like this,

import time
import multiprocessing
i = True
upgrade_level = 0
money = 10

def clear_chat():
  num = 0
  while num < 100:
    print(" ")
    num += 1




def loop1():
  global money
  while i:
    question = input("Would you like to upgrade? (y)\n")
    if(question.lower() == "y"):
      if(money >= 10):
        clear_chat()
        money = money-10
        print("You upgraded once")
        print("Your upgrade level is " + str(upgrade_level))
      elif(money < 10):
        clear_chat()
        print("You do not have enough money ($10) to buy an upgrade")
    else:
      clear_chat()
      print("You can only input (y)")
  
def loop2():
  global money
  while i:
    money += 10
    time.wait(10)

p1 = multiprocessing.Process(target = loop1())
p2 = multiprocessing.Process(target = loop2())

Can read this for examples https://www.w3schools.com/python/python_variables_global.asp

Solution 3:[3]

You need to specify that the variable 'money' in both functions is the global one you initialized beforehand using the keyword global. Like this:

import time
import multiprocessing
i = True
upgrade_level = 0
money = 10

def clear_chat():
  num = 0
  while num < 100:
    print(" ")
    num += 1

def loop1():
  global money
  while i:
    question = input("Would you like to upgrade? (y)\n")
    if(question.lower() == "y"):
      if(money >= 10):
        clear_chat()
        money = money-10
        print("You upgraded once")
        print("Your upgrade level is " + str(upgrade_level))
      elif(money < 10):
        clear_chat()
        print("You do not have enough money ($10) to buy an upgrade")
    else:
      clear_chat()
      print("You can only input (y)")
  
def loop2():
  global money
  while i:
    money += 10
    time.wait(10)

p1 = multiprocessing.Process(target = loop1())
p2 = multiprocessing.Process(target = loop2())

However, note that the i variable is never changed to False and that creates an infinite loop. Check this article for more info on the error: Article

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 anarchy
Solution 3 Yara Elkady