Python Forum
I'm getting a NameError: ...not defined.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm getting a NameError: ...not defined.
#1
Hi, I'm getting a "NameError: name 'name' is not defined" error when running my new test code. It's my first day of using Visual Studio Code with Python and the 'name' function (not entirely sure if you call 'name' a function..) has been working earlier but now, for some reason, it no longer seems to register.


This is my source code:

if name == 'Sam':
print('Hi Sam')
print('Are you good?')


and this is the output I get when I Run the code using the VSC extension 'Code Runner':

[Running] python3 -u "/Users/eric/Desktop/Python Programs test/if name == 'Sam':.py"
Traceback (most recent call last):
File "/Users/eric/Desktop/Python Programs test/if name == 'Sam':.py", line 1, in <module>
if name == 'Sam':
^^^^
NameError: name 'name' is not defined

[Done] exited with code=1 in 0.068 seconds





Sorry if my question is badly formulated or if I'm not posting this correctly, this is like I said my first day and also my first time ever posting in a coding forum. If you have pointers for me to correct please let me know, I'm all ears for feedback!


I'm using macOS Ventura version 13.6.1
Visual Studio Code version: 1.87.2 (Universal)
and Python 3.12.2
to point out just incase, I do believe I installed a version of Python a few years ago when I was trying to learn some coding but I'm not sure if that was on this same Mac or my old one. Not sure how to see if I have multiple versions of Python installed or not.
Reply
#2
Please use bbtags when posting code.
The error is saying that you need to define a value for name.
Add name = '' above the if statement.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Mar-23-2024, 09:37 PM)menator01 Wrote: Please use bbtags when posting code.
The error is saying that you need to define a value for name.
Add name = '' above the if statement.

Great thank you! haha, easy but was too tired to see that miss so my mind wandered and thought it was a big problem..

Thanks for pointing that out, didn't know how but say your link there 👍
Just testing it out with the corrected program now to see how it looks:

name = 'Sam'
if name == 'Sam':
    print('Hello Sam')
    print('Are you good?')
else:
    print('Hello there')
Have a nice day!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting NameError for a function that is defined JonWayn 2 1,122 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,914 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,353 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,522 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,935 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,376 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 15,034 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,406 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,902 Jul-26-2021, 04:36 PM
Last Post: snippsat
  I am getting a NameError that is not defined and not sure why it happen rick0922 5 4,111 Jun-14-2021, 03:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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