Posts: 4
Threads: 1
Joined: Apr 2019
Hello Everyone,
I am new to Python and have recently started working on it.
Please help me resolve the error
My code is
def hello():
print("Hello Everyone")
hello() Error is
>>> hello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined Please let me know where I am wrong. Thank You
Posts: 10
Threads: 2
Joined: Apr 2019
You are probably using python 2 and this is the syntax for python 3 interpreter. :D
Posts: 1,950
Threads: 8
Joined: Jun 2018
In interactive interpreter it works like this:
>>> def hello():
... print('Hello everyone')
...
>>> hello()
Hello everyone However, if your code in file then you should run the file.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 4
Threads: 1
Joined: Apr 2019
Please see the complete message from Terminal
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\le851ds\Desktop\Python Blockchain> & C:/Users/le851ds/AppData/Local/Programs/Python/Python37-32/python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> hello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined
Posts: 10
Threads: 2
Joined: Apr 2019
In the last one, i dont see your def.
Posts: 4
Threads: 1
Joined: Apr 2019
(Apr-11-2019, 07:45 AM)moste Wrote: In the last one, i dont see your def. 
The code is
def hello():
print("Hello Everyone")
hello() The message in terminal is
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\le851ds\Desktop\Python Blockchain> & C:/Users/le851ds/AppData/Local/Programs/Python/Python37-32/python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> hello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'hello' is not defined
>>>
Posts: 10
Threads: 2
Joined: Apr 2019
ok now i got you!
You have to execute the script file via terminal (type: python3 my_script.py)
The def hello() is in your file, trying to use this def from the interpeter, without opening the file, will never work!
I'm sorry for my poor english :P
I hope, that i helped you!
Posts: 7,313
Threads: 123
Joined: Sep 2016
What editor/shell are using to run this?
The script is not connect or in same folder as the interactive interpreter run.
Different Editors/shell can deal with this different,some dos it automatic and some dos not.
Just from command line can use -i .
python -i my_script.py or a better interactive interpreter,like ipython or ptpython eg ptpython -i my_script.py .
Posts: 4
Threads: 1
Joined: Apr 2019
I am sorry I couldn't really understand the solutions as I new to coding.
However, when I Started Debugging from the drop down options above, I got the following message
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\le851ds\Desktop\Python Blockchain> cd 'c:\Users\le851ds\Desktop\Python Blockchain'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'C:\Users\le851ds\AppData\Local\Programs\Python\Python37-32\python.exe' 'c:\Users\le851ds\.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\ptvsd_launcher.py' '--default' '--client' '--host' 'localhost' '--port' '51587' 'c:\Users\le851ds\Desktop\Python Blockchain\tryingblockchain.py'
Hello Everyone I will keep exploring.
Thank you for your help.
Appreciate it.
Posts: 8,156
Threads: 160
Joined: Sep 2016
check this tutorial
you mix executing python script from command line/terminal and using python interactive mode
https://python-forum.io/Thread-How-to-Ex...ython-code
|