0

This function newdef() works flawlessly in interpreter, but when executed as a file it throws a NameError. Can someone tell me why?

myuser$ cat r2.py
#!/usr/bin/env python

def newdef():
    a = input('say yes\n')
    if a == 'yes':
        print('he said yes')
    else:
        print('he said something else')


newdef()

Executed from commandline:

myuser$ ./r2.py
say yes
yes
Traceback (most recent call last):
  File "./r2.py", line 11, in <module>
    newdef()
  File "./r2.py", line 4, in newdef
    a = input('say yes\n')
  File "<string>", line 1, in <module>
NameError: name 'yes' is not defined
myuser$

If i copy / paste the function into iPython interpreter and execute it there it works.

zimba
  • 51
  • 3
  • 1
    Oh no... you're using the _old_ python... :( – cs95 Mar 29 '18 at 04:48
  • woh, thx... seems you're right. When i change the shebang to '/usr/bin/env python3' it works... i wasn't aware of this version quirks anymore as a i have an alias python='python3.6' line in my .bashrc, and until now i didn't have such problems... – zimba Mar 29 '18 at 05:03

0 Answers0