Set-up Python

*** The link relating to the section of the course I am on “Set-up Python”: https://www.codecademy.com/articles/setup-python ***

Section: “Running Python”

Context: I am using BBEdit in conjunction with the Terminal application. I am trying to execute the “first.py” step in this section. I have typed in the code as instructed

-------- books = [‘Harry Potter and the Prisoner of Azkaban’, ‘ The Road’, ‘The White Tiger’]

for novel in books:
print(novel) ---------

Context cont.: However, when I type in the in terminal (as it says to do in the article) “python first.py” this is the error that appears in the terminal:

Michaels-MacBook-Pro:Documents michael$ python first.py

File “first.py”, line 1

SyntaxError: Non-ASCII character ‘\xe2’ in file first.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Michaels-MacBook-Pro:Documents michael$

QUESTION: Can anyone figure out why this is and how I fix it please?

Try typing python at the command prompt to see which version of python you are using. It will start python, probably version 2.7.x. Then type your code in being sure to indent the print statement. You have encoded as though you are using python version 3.

books = ['Harry Potter and the Prisoner of Azkaban', 'The Road', 'The White Tiger']

for novel in books:
  print(novel)

Hi there.

@harrjt is on the right lines with the encoding. The article you’re following uses Python 2, which treated strings as ASCII encoded by default and if you wanted to use a larger character set - like Unicode - then you had to explicitly tell Python about it.

What’s confusing me, however, is that the codepoint \xe2 is the character â… which, as far as I can see, is not present in the code snippet you’ve posted.

Is that the entirety of what you have written in first.py? If not, can you please post all of it, making sure to correctly format your code by using the </> icon in the menu bar which appears at the top of the text box while you type.

capture

Press Enter to go to a blank line, click that icon, and you will see this:

capture_r

Copy everything from your first.py file into that highlighted section and we’ll be able to see what you’re running.

The fix, incidentally, is either to remove the offending â character (which we can’t see) or to put the following on the very first line of your program:

# coding: utf-8

Thank you for your response. The print statement was indented, but I accidentally didn’t do it in the post, my mistake. - I will take a picture next time to avoid the same error. In the end, I think I figured it out. I believe it had something to do with the version of python. I later realized that the article was for python 2.7 or earlier while I was using version 3.7. I should give update when I am sure.

1 Like

Thank you. That was the all the code in the first.py file. I made a mistake in typing the code in my initial question, the print statement was actually indented. I’ll be sure to format future questions with the instructions you provided. I think the main issue was me using Python 3, while the article, as you stated, was for Python 2.

1 Like