Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Level - Name Error
#1
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
Reply
#2
You are probably using python 2 and this is the syntax for python 3 interpreter. :D
Reply
#3
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.
Reply
#4
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
Reply
#5
In the last one, i dont see your def. Angel
Reply
#6
(Apr-11-2019, 07:45 AM)moste Wrote: In the last one, i dont see your def. Angel

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
>>>
Reply
#7
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!
Reply
#8
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.
Reply
#9
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.
Reply
#10
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner Python Error ianmac88 4 1,305 Sep-05-2022, 12:30 PM
Last Post: jefsummers
  Code not reading http link from .txt file (Beginner level) plarrip 3 2,406 Dec-17-2020, 11:33 PM
Last Post: bowlofred
  Beginner having Syntax Error problem RyanHo 3 2,363 Sep-10-2020, 08:33 AM
Last Post: cnull
  Unindent does not match any outer intendation level -error MaartenRo 3 2,272 Jun-28-2020, 11:46 AM
Last Post: MaartenRo
  Beginner - simple package installation error mefeng2008 0 1,719 Mar-13-2020, 09:17 AM
Last Post: mefeng2008
  [split] Python beginner: Weird Syntax Error mnsaathvika 1 2,127 Jul-22-2019, 06:14 AM
Last Post: buran
  Beginner Getting an error Boa 5 3,720 Jun-22-2019, 11:46 PM
Last Post: metulburr
  Python beginner: Weird Syntax Error mentoly 5 10,320 Oct-13-2017, 08:06 AM
Last Post: gruntfutuk
  Display error, beginner help :( Armandas 1 3,186 May-02-2017, 08:39 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020