This question already has answers here:
Using global variables in a function
(25 answers)
Closed 8 years ago.
I've been having trouble with the "def" function. I know this question has already been asked, but the answer didn't satisfy me and I didn't see how to apply it to my code. I'm trying to make the the popular game 2048 in Python. Basically, when I define the function that makes the entire board move left, it bites me with the error: UnboundLocalError: local variable referenced before assignment. It seems that I have to define the variables "bone" and "btwo" in somewhere that isn't, like, global. But I am yet to figure out how to get that working. Setting parameters in my moveleft() function isn't working, e.g moveleft(bone,btwo). So I'm at my wit's end.
Now, I'll include the entire code, all commented up, but the part I think has the problem is where I define the function moveleft(). Also, if there are any outstanding stupid bits of code, please tell me. Also, try and keep it simple, I'm pretty rubbish with programming and its associated phrases and terms. This is only my 3rd attempt at a code.
I realise I'm asking a lot, but I would really, really, appreciate help with this.
Code:
http://pastebin.ca/2824228
Minimized version:
bone, btwo = 1, 2
def move_left():
if bone == 1: print("bone is 1")
if btwo == 2: print("btwo is 2")
btwo = 3
bone = 2
move_left()
If you are writing to a global variable inside a function, then you need to explicitly say that you are referring to a global variable. So put this as the first line in your function:
global bone, btwo, bthree, bfour, bfive, bsix, bseven, beight, bnine
And, why don't you use a list instead of defining 9 variables?
Related
This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Closed 12 days ago.
Im playing around with functions and I never was able to understand why I couldn't change a variable. It always gives me an error.
Iv'e tried googling it but nothing really worked. Could someone help me out with this?
x = 1
def run():
print(x)
x += 1
run()
Since you are assigning to x, you need to declare it as global.
def run():
global x
print(x)
x += 1
Otherwise, the assignment makes it a local variable, one you try to print before assigning to it (and one that isn't initialized before you try to increment it).
This question already has answers here:
How to restore a builtin that I overwrote by accident?
(3 answers)
Closed 2 years ago.
I am new to python, and realized that i can assign print i.e. an inbuilt function as a variable, then when i use print('hello world')this shows the exact error that i faced
I am familiar to c++ and even in that we were never allowed to use an inbuilt function as a variable name.
those were the fundamental rules for naming a variable
If python.org has issued the new version I'm sure they would have done it for a reason, bbut i want to know how do i access my print statement after assigning a value to it?
you won't be able to access your print function unless you do hacky things, which I recommend not to do them in the middle of your code.
Also it is good to know that python (as c++) has scopes for variables, and variables "die" and they are no longer accessible when scope ends. For instance:
def change_print_value():
print = 3
change_print_value()
print('Print works as expected')
It is a good practice to avoid using reserved keywords as variable names. Any IDE has the keywords highlighted, so you can easily realize when you are using a keyword where you shouldn't.
print is not part of the reserved keywords list in python. Here's a comprehensive list of reserved words.
Functions are first class objects in python, so that means they can be treated and manipulated as objects. Since print is a function (and an object), when you call print = 1, you reassign the variable print to have a value of 1, so the functionality "disappears".
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I am a beginner in python coding. I have a couple of questions:
Question 1: How Can I define multiple variables using for-loop in Python
global I1_1
global I1_2
global I1_3
global I1_4
global I1_5
global I1_6
Questions 2: How can I call multiple objects using the for-loop.
For example, I have these objects:
lineEdit_1
lineEdit_2
...
lineEdit_100
I would like to call everyone of them in a for loop. I tried to look for something similar online but I didnt find. My first thought was to define a range for a variable i (1-100) then call lineEdit_i. Any idea?
It sounds like you're talking about dynamically creating variables, which to my understanding you cannot do. What you can do, however, is leverage the python data structure of lists. Initialize a list by setting a variable like my_list = [] then you can .append() any of your variables to the list, use a for-loop to loop through all your variables, and do whatever you need to in your loop.
This question already has answers here:
How do I do multiple assignment in MATLAB?
(9 answers)
Closed 9 years ago.
I've noticed that in a python code you can do something like:
a=0
b=1
a,b=b,a
print(a,b)
which outputs (a=1,b=0) (i.e. each variable is assigned independently of the last assignment). Is there a way to do something similar in MATLAB?
Sorry if this is a really simple question, but I've been trying to find a clean answer to this for a bit of time now and haven't found anything.
There is no need for an additional temporary variable here. If you want multiple assignments in a single statement, you can use deal:
[a, b] = deal(b, a)
I believe this is what you're looking for.
It's always possible to do that with a temporary variable in any language. The unpacking method of Python is just a little syntax sugar to simplify the developer life :)
a = 0
b = 1
tmp = a
a = b
b = tmp
print(a,b)
Anyway, it's not magical, the Python byte code might implement the permutation with a temporary variable. (There's techniques to do this without temp variable, but well... for the sake of clarity use one :p)
This question already has answers here:
What's the scope of a variable initialized in an if statement?
(7 answers)
Closed 3 years ago.
In Python, are variable scopes inside if-statements visible outside of the if-statement? (coming from a Java background, so find this a bit odd)
In the following case, name is first defined inside the if-block but the variable is visible outside of the if-block as well. I was expecting an error to occur but 'joe' gets printed.
if 1==1:
name = 'joe'
print(name)
if statements don't define a scope in Python.
Neither do loops, with statements, try / except, etc.
Only modules, functions and classes define scopes.
See Python Scopes and Namespaces in the Python Tutorial.
Yes, in Python, variable scopes inside if-statements are visible outside of the if-statement.
Two related questions gave an interestion discussion:
Short Description of the Scoping Rules?
and
Python variable scope error
All python variables used in a function live in the function level scope. (ignoring global and closure variables)
It is useful in case like this:
if foo.contains('bar'):
value = 2 + foo.count('b')
else:
value = 0
That way I don't have to declare the variable before the if statement.