0

For a class of mine I have to make a very basic calculator. I want to write the code in such a way that the user can just enter what they want to do (ex. √64) press return and get the answer. I wrote this:

if '√' in operation: squareRoot = operation.replace('√','') squareRootFinal = math.sqrt(int(squareRoot))

When I run this in IDLE, it works like a charm. However when I run this in Terminal I get the following error:

SyntaxError: Non-ASCII character '\xe2' in file x.py on line 50, but no encoding declared;

any suggestions?

Zardoz
  • 39
  • 1
  • 7

1 Answers1

0

Just declare the encoding. Python is begin a bit cautious here and not guessing the encoding of your text file. IDLE is a text editor and so has already guessed the encoding and stored things internally as unicode, which it can pass directly to the Python interpreter.

Put

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

at the top of the file. (It's pretty unlikely nowadays that your encoding is not UTF-8.)

For reference, in past times files had a variety of different possible encodings. That means that the same text could be stored in different ways in binary, when written to disk. Almost all encodings have the same interpretation of bytes 0 to 127—the ASCII subset. But if any other bytes occur in the file, their meaning is potentially ambiguous.

However, in recent years, UTF-8 has become by far the most common encoding, so it's almost always a safe guess.

Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67