Code works in python environment, but not in codecademy


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/1/2?curriculum_id=4f89dab3d788890003000096


The code gives an error when entering the first hobby:
Which hobby do you have? Chess
Traceback (most recent call last):
File "python", line 8, in
File "", line 1, in
NameError: name 'Chess' is not defined


hobbies = []

# Add your code below!
for i in range(3):
    hobby = ''
    while hobby == '':
        hobby = input('Which hobby do you have? ')
    hobbies.append(hobby)


#2

Found the cause of the error:
The statement "raw_input" should be used instead of "input" -- in line 8.
Strange that it works when running the code in a python 3.5 environment.


#3

Hi @orionsgfm ,

When your code worked properly, you must have been using Python 3.x. This works as intended in that version of Python ...

hobby = input('Which hobby do you have? ')

However, you should nearly always use this raw_input instead of input in all versions of Python prior to 3.x, for example, as follows ...

hobby = raw_input('Which hobby do you have? ')

Codecademy uses Python version 2.7.x.

EDIT: (July 9, 2016): I just saw your second post. If you use input in version 2.7.x, it will attempt to interpret what the user enters as a Python expression. So, when you enter Chess, it will look for an object named Chess, such as a variable, and then report back that it is undefined. Solution: use raw_input.


#4

Ok, thanks for the clarification!


#5

By the way, do you know why codecademy uses Python version 2.7?

I had to make a decision whether to install Python 2 or 3 and read some articles -- I found no good reason as someone learning the language to use an older version. It seems wrong for codecademy to force users onto and old version, when the new one is available for the future and and all the important public libraries (e.g. NumPy and SciPy) are available in Python 3.


#6

You can install both on the same machine, and they coexist well. In fact, I have versions 2.7, 3.5, and an alpha version of 3.6 on my Mac

Codecademy created the Python track in 2012, when fewer libraries had been ported to Python 3 than have been ported as of today. It is useful to know both version 2 and 3, since there is much existing code that was written for version 2, and version 3 is current. If you learn either version 2 or 3, it is easy to learn the other.


#7

Ok, I will install version 2 as needed.

Thanks for giving the historical aspect.
At this point in time, codecademy could even consider adding a course section where on learn about the differences between version 2 and 3, giving the users the background and present situation.


#8

Thanks for your suggestions, @orionsgfm . As a moderator, I volunteer to help users with code, but Codecademy headquarters is the venue where decisions are made regarding what courses to offer.

You may wish to vote in Codecademy's ongoing Course Poll, where their staff can become informed about what courses the users would like them to develop. You can also post there to specify reasons why they should offer material on Python 3.


#9

Thanks for the reference. I have entered my vote and suggestion.


#10

I prefer Python 2: Python 3 was not really an upgrade in my opinion, just another path


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.