Jika
Jika

Reputation: 1584

NameError in Pycharm?

I wrote this code in Pycharm and I got the following error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'crypte' is not defined

__ My code:

alphaclair = "abcdefghijklmnopqrstuvwxyz"
alphadecale = "defghijklmnopqrstuvwxyzabc"

def modif(lettre):
    for i in range(0, len(alphaclair)):
        if alphaclair[i]==lettre:
            return alphadecale[i]

def crypte(phrase):
    string = ""
    for i in range(0, len(phrase)):
        if phrase[i]==" ":
            string = string + " "
        else:
            string = string + modif(phrase[i])
    return string

When I do runand then I execute it in the Python console I get that error.

When I select the code and I right click and do Execute Selection in Console I do not get the error.

What is my mistake?

Upvotes: 3

Views: 10318

Answers (3)

KevinH
KevinH

Reputation: 21

Another source of this error is related to the PyCharm watchlist.

If there is an item in the watchlist that isn't defined in the Python script, PyCharm will generate a NameError.

Upvotes: 1

Annonymous
Annonymous

Reputation: 29

switch your interpreter from python 2.7 to 3.1 and will solve the problem, you can do this by going to the settings of the ide or calling python 3.(version) in command line

Upvotes: 2

Krimson
Krimson

Reputation: 7664

When you click run (in pycharm) it executes the file in a different instance of python for debugging and all that. The python console window, is totally different. Its not linked to your code. Its there to execute python commands and tests stuff just like you would do in IDLE.

When you do Execute Selection in Console, this basically executes your file in the console instead of running it on its own.

Its something like this you would do in IDLE when you want to execute your file: exec(open("mycode.py").read())

Edit: In your python console, you can execute the above command to load your file in the console. But this is basically the same thing as selecting Execute Selection in Console

Upvotes: 4

Related Questions