Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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())

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First time.wait() is accully time.sleep().

The main problem in your code is, you haven’t added the global keyword in function. you can read the global variable without global keyword, but in order to make changes in it you have to use global keyword, i.e use global money in both loops.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading