0

I have a function (calculate) that references a variable outside of it (fill and empty) and it just won't detect that the variable already exists and tries to create one but I need that result for another function (render). Here is my code:

render = 0
isRunning = True
fill = ""
empty = ""
os.system('clear')

def calculate():
  for x in position:
    render = x
    if render == 0:
      empty = "-"
    if render == 1:
      fill = "⬛"

def render():
  if position[0] == 1:
    print(fill, empty)
  if position[0] == 0:
    print(empty, fill)
os.system('clear')

while isRunning:
  calculate()
  render()
  time.sleep(3)

  os.system('clear')

This is my new code after Pranav Hosangadi. I was confused on how to pass arguments into a function because I'm new to Python. This still doesn't work:

import time
import os
ant = "#"
position = [0, 1, 0]
render = 0
isRunning = True
fill = ""
empty = ""

def calculate(empty = empty, fill = fill):
  for x in position:
    render = x
    if render == 0:
      empty = "-"
    if render == 1:
      fill = "⬛"
  return fill, empty

def render_function():
  if position[0] == 1:
    print(fill, empty)
  if position[0] == 0:
    print(empty, fill)
os.system('clear')

while isRunning:
  calculate()
  render_function()
  time.sleep(3)
  os.system('clear')

Any help would be great.

4
  • 4
    Your function render() overwrites the integer render = 0? Even if you fix the problem of referencing the global variable, nothing will happen when you do calculate() because render is a function, not an integer, and is never equal to 0 or 1. Everybody is telling you to use global variables, but that isn't always the best practice. You should just pass the value of your integer to the function as an argument instead, and use return to return a value back. Commented Oct 26, 2021 at 15:59
  • 4
    As an aside, most modern languages will at least make it uncomfortable to reach outside the scope of a function and modify variables/names. In python's case, the global keyword. This is done for good reason as modifying global state within a function makes your code much more difficult to reason about. Commented Oct 26, 2021 at 16:02
  • 1
    Does this answer your question? Python Global Variable not updating Commented Oct 26, 2021 at 16:04
  • The render variable seems to serve no purpose other than to copy the loop variable in that loop. Commented Oct 26, 2021 at 16:29

2 Answers 2

1

You need to use the global keyword:

x = "awesome"

def myfunc():
  x = "fantastic"
  print("Python is " + x)

myfunc()

print("Python is " + x)

returns

Python is fantastic
Python is awesome

using global,

x = "awesome"

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)
Sign up to request clarification or add additional context in comments.

Comments

0

Use global when modifying global variables (variables that you define in the top-level) inside functions:

def calculate():
  global render
  for x in position:
    render = x
    if render == 0:
      global empty
      empty = "-"
    if render == 1:
      global fill
      fill = "⬛"

2 Comments

This fixes the result of the problem. The problem is really using globals when they aren't needed in the first place though.
Also the function render() overwrites the integer render = 0?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.