-1

I tried this how to tell ubuntu to use python 3.4 instead of 2.7? but nothing.. I have a problem with the nonlocal

This is my code:

x = 50   
def func_outer():  
     x=2  
     print('x is '), x

     def func_inner():
        nonlocal x
        x=5
    func_inner()
    print('the local x changed to '),x

=>

    nonlocal x
             ^
SyntaxError: invalid syntax

Do you know how to change it to python 2.7 or any alternative about nonlocal?

George
  • 1

2 Answers2

1

Such file:

#!/usr/bin/env python3 
x = 50   
def func_outer():  
     x=2  
     print('x is '), x

     def func_inner():
        nonlocal x
        x=5
     func_inner()
     print('the local x changed to '),x

when run as ./file.py yields no errors with python3.5 on Ubuntu 16.04.

enedil
  • 982
  • 5
  • 15
  • 27
0

Well i found a solution finally!( How stupid problem) :(
At first , i was created the file (test.py) with nano. My first command was #!/bin/usr/python then i saved my file. Every time i wanted to change the code i opened the test.py with the editor and i wrote the code there, then i just opened the terminal writing "python test.py" to run my script. This is why i couldn't run my code in python 3. I made a new file and instead of #!/bin/usr/python and i wrote #!/bin/usr/python3. The script now running in python 3.

George
  • 1