You are not logged in.

#1 2010-05-25 16:33:11

samfoy
Member
Registered: 2010-05-19
Posts: 14

[solved]Issue trying to learn python3

Hey, im quite new to programmin and decided python would be a good first language to try. I've been following this tutorial http://www.swaroopch.com/files/byteofpy … n_v192.pdf.

Any ways, i came to this section teaching about the while loop and break command and entered this as break.py

#!/usr/bin/python
# Filename: break.py
while True:
    s = (input('Enter something : '))
    if s == 'quit':
        break
    print('Length of the string is', len(s))
print('Done')

when i try to run the program i get this error:

[sam@desktop ~]$ python break.py 
Enter something : hello
Traceback (most recent call last):
  File "break.py", line 4, in <module>
    s = (input('Enter something : '))
  File "<string>", line 1, in <module>
NameError: name 'hello' is not defined

can anyone direct me to what may be the issue here?, im entering the code exactly as in the tutorial....

Last edited by samfoy (2010-05-25 21:15:33)

Offline

#2 2010-05-25 16:48:38

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: [solved]Issue trying to learn python3

Are you sure you have python 3 running the program?
Run python, and check the version.
If the program only work with python 3 that can be the problem.

Edit: with the 2.6.4 version i get the same error.

Last edited by n0dix (2010-05-25 16:52:20)

Offline

#3 2010-05-25 17:13:59

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [solved]Issue trying to learn python3

Try using raw_input instead of input.  'Input' results I think are not interpreted the same way as 'raw_input'.

Scott

Offline

#4 2010-05-25 17:38:04

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [solved]Issue trying to learn python3

firecat53 wrote:

Try using raw_input instead of input.  'Input' results I think are not interpreted the same way as 'raw_input'.

Scott

The pdf mentions this link: http://www.python.org/dev/peps/pep-3111/
So once you get python 3 like n0dix says, the code should work.

Offline

#5 2010-05-25 17:52:04

austin.rbn
Member
Registered: 2010-01-23
Posts: 77

Re: [solved]Issue trying to learn python3

Make sure you either execute it by invoking the interpreter directly, i.e. "python3 break.py", or by specifying python3 in the shebang line, as "#!/usr/bin/env python3". The latter is generally better practice, I think.

It works fine for me with python3. No error.


"Computer Science is embarrassed by the computer." -- Alan J. Perlis

Offline

#6 2010-05-25 17:53:06

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: [solved]Issue trying to learn python3

n0dix is correct - just to clarify, you must install the python3 package and change the first line to #!/usr/bin/python3.

The problem arises because prior to Python 3, the input([prompt]) function is equivalent to eval(raw_input([prompt string])). As you may know, the eval function tries to run the given string as Python code, so it thinks you are trying to read a variable called hello. You can find more of this kind of information in the documentation section on python.org.

As a side note, this looks kind of dangerous to me, since Python 3 code is definitely going to end up being accidentally run by a Python 2 interpreter at some point. In this case it allows arbitrary code execution.

Offline

#7 2010-05-25 17:58:25

samfoy
Member
Registered: 2010-05-19
Posts: 14

Re: [solved]Issue trying to learn python3

Thanks to all, i had installed python3, i see my shebang just called for python though. i changed first line to #!/usr/bin/python3.

p.s. for future reference, would it be better to start writing she bang as #!/usr/bin/env python3 rather than /usr/bin/python3?

Offline

#8 2010-05-25 18:05:08

austin.rbn
Member
Registered: 2010-01-23
Posts: 77

Re: [solved]Issue trying to learn python3

samfoy wrote:

Thanks to all, i had installed python3, i see my shebang just called for python though. i changed first line to #!/usr/bin/python3.

p.s. for future reference, would it be better to start writing she bang as #!/usr/bin/env python3 rather than /usr/bin/python3?

The benefit of calling env here is that it will execute python3 even if it is installed somewhere other than "/usr/bin/". See "man env".


"Computer Science is embarrassed by the computer." -- Alan J. Perlis

Offline

#9 2010-05-25 18:14:30

samfoy
Member
Registered: 2010-05-19
Posts: 14

Re: [solved]Issue trying to learn python3

austin.rbn wrote:
samfoy wrote:

Thanks to all, i had installed python3, i see my shebang just called for python though. i changed first line to #!/usr/bin/python3.

p.s. for future reference, would it be better to start writing she bang as #!/usr/bin/env python3 rather than /usr/bin/python3?

The benefit of calling env here is that it will execute python3 even if it is installed somewhere other than "/usr/bin/". See "man env".

I see, so if I wanted to make my python script portable to other *nix systems, #!/usr/bin/env python3 would do that provided they had python3 installed on their system?

Offline

#10 2010-05-25 19:49:14

austin.rbn
Member
Registered: 2010-01-23
Posts: 77

Re: [solved]Issue trying to learn python3

samfoy wrote:
austin.rbn wrote:
samfoy wrote:

Thanks to all, i had installed python3, i see my shebang just called for python though. i changed first line to #!/usr/bin/python3.

p.s. for future reference, would it be better to start writing she bang as #!/usr/bin/env python3 rather than /usr/bin/python3?

The benefit of calling env here is that it will execute python3 even if it is installed somewhere other than "/usr/bin/". See "man env".

I see, so if I wanted to make my python script portable to other *nix systems, #!/usr/bin/env python3 would do that provided they had python3 installed on their system?

Yes. That's the idea, anyway. Of course, most people are going to have it installed in the same place, so it isn't a big deal.

Last edited by austin.rbn (2010-05-25 19:49:51)


"Computer Science is embarrassed by the computer." -- Alan J. Perlis

Offline

#11 2010-05-25 19:53:31

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: [solved]Issue trying to learn python3

Glad that you solve the problem.

I supposed the thread can be mark as solved. Can you do that samfoy?

Offline

#12 2010-05-25 21:16:01

samfoy
Member
Registered: 2010-05-19
Posts: 14

Re: [solved]Issue trying to learn python3

n0dix wrote:

Glad that you solve the problem.

I supposed the thread can be mark as solved. Can you do that samfoy?

done smile
thank you all

ps: sorry for my noobness. smile

Last edited by samfoy (2010-05-25 21:18:56)

Offline

Board footer

Powered by FluxBB