global-variables

How can a function access variables that are not defined inside the function?

How can a function access variables that are not defined inside the function? Question: I recently started studying Python and I came across an example that I did not understand: def teste(): print(a, b) a = 5 b = 4 teste() # Outputs ‘5 4’ What is happening here? Is teste() able to access a …

Total answers: 1

How to use a variable across multiple functions

How to use a variable across multiple functions Question: I am trying to use two variables (i, and q) across three functions. I want to test whether i and q are valid after each input . If they are then they will be added to list and the while loop continues. If they are not …

Total answers: 2

Why doesn't the value of my global variables change when I'm inside a function call?

Why doesn't the value of my global variables change when I'm inside a function call? Question: So the logic is pretty lengthy but it’s pretty much several trackbars changing certain values which should be assigned to certain variables inside the while loop that captures video. This is one of the trackbar functions with the variables …

Total answers: 1

Is there a way to run a boolean if statement in Python Turtle?

Is there a way to run a boolean if statement in Python Turtle? Question: I’m editing a Turtle Pong template and realized that two paddles can’t move at the same time, so I’m trying to fix that bug by having it so, when a key is pressed, a boolean is set to true, and that …

Total answers: 1

Can you add 2 global variables work inside a function while inside a while loop?

Can you add 2 global variables work inside a function while inside a while loop? Question: I am trying to build a function with a loop inside. import time #example def infiniteloop2(): while True: print("hi") time.sleep(1) infiniteloop2() One thing I encountered was errors in adding 2 global variables. import time x=7 y=3 #example def infiniteloop2(): …

Total answers: 1

How to understand global and local scopes in Python?

How to understand global and local scopes in Python? Question: I am a novice in Python and wondering the situation below. x = 1 def func(): print(x) x = 2 return x So I got the UnboundLocalError: local variable ‘x’ referenced before assignment. But if I right understand – Python read and execute code row …

Total answers: 1

How to simplify my basic python coding higher/lower game?

How to simplify my basic python coding higher/lower game? Question: I’m a beginner at Python and I wanted to try out my own little project based off the things I’ve learnt. It’s a higher/lower game where the user gets a starting number and then has to guess if the next number is higher or lower …

Total answers: 1

How scope is determined in Python

How scope is determined in Python Question: Why does the first print statement in the second function throw an error that x is not defined? x = 5 def function_a(): print(x) def function_b(): print(x) x = 7 print(x) Running the first function gives the following result. >>> function_a() 5 While running the second function throws …

Total answers: 4