Permanently changing an outside variable inside a function
<p>I am a huge beginner, but I have a variable that has a value, and I'd like to change it within a function, so that outside of the function the variable is permanently changed. Searching around I've found some information on how to access outside variables (I think their called global variables), but not permanently modifying them in any way. </p>

<p>Here's a little bit of code to <em>represent</em> what it is I'm trying to do:</p>

<pre><code>x = 0

def variableChanger():
    global x
    x =+ 1

variableChanger()
print(x)
variableChanger()
print(x)
variableChanger()
print(x)
</code></pre>

<p>The idea is that it would have the console output...</p>

<pre><code>&gt;&gt;&gt; 1
&gt;&gt;&gt; 2
&gt;&gt;&gt; 3
</code></pre>

<p>since it is changing the global <code>x</code> variable by adding one to it. But instead it is creating a new global variable called <code>x</code>, and setting it's value to <code>1</code> every time I run the <code>variableChanger()</code> function, and I just end up with...</p>

<pre><code>&gt;&gt;&gt; 1
&gt;&gt;&gt; 1
&gt;&gt;&gt; 1
</code></pre>

<p>What I want to do is access the variable <code>x</code> that's at the top of the code and increment that, so that whenever and wherever I access it later, it has the incremented value. </p>

<p>Is there a way to do that?</p>

<p>This is because <em>you do</em> re-assign <code>x</code> every time. <code>x =+ 1 != x += 1</code>. <code>x =+ 1</code> assigns <code>x</code> to <code>1</code>; <code>x += 1</code>, the <em><a href="https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements" rel="nofollow">augmented assignment statement</a></em>, increments <code>x</code> by one:</p>

<pre><code>x = 0

def variableChanger():
    global x
    x += 1

variableChanger()
print(x)
variableChanger()
print(x)
variableChanger()
print(x)
</code></pre>

python python-3.x global-variables