Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError in Pycharm?

Tags:

python

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?

like image 752
Jika Avatar asked Apr 10 '26 23:04

Jika


2 Answers

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

like image 185
Krimson Avatar answered Apr 13 '26 12:04

Krimson


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

like image 31
Annonymous Avatar answered Apr 13 '26 13:04

Annonymous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!