Python Forum
Variable not defined even though it is
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable not defined even though it is
#1
Question 
Hi! Just started my coding journey and I am immediately running into an error. I am currently reading the book "Python Crash Course: A Hands-on, Project based Introduction to Programming" and am mimicking the examples on VSCode as I read along.

I am currently trying out this example from the book:

message = "hello world"
print(message)
Upon running this in VSCode, I keep getting this error:

Output:
>>> print(message) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'message' is not defined
What am I doing wrong?
Reply
#2
Looks like you running python from a cmd line in shell and not the script itself.
either do python script.py or if in the interpreter do message = 'my message' then print(message)
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-28-2024, 07:14 AM)menator01 Wrote: Looks like you running python from a cmd line in shell and not the script itself.
either do python script.py or if in the interpreter do message = 'my message' then print(message)

Thank you for this. I figured that I just needed to highlight the entire code, then press shift + enter to run it. Previously, I was only pressing shift + enter without highlighting any line of code. This was effectively running just the last line of code.
Reply
#4
you should be able to right click code, select python, and run in terminal
first you need to (one time) press ctrl-P in code window and select interpreter to be used.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 601 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,328 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,145 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Function will not return variable that I think is defined Oldman45 6 3,541 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,287 Aug-12-2020, 10:34 PM
Last Post: snippsat
  Variable not defined Heyjoe 4 2,577 Jul-10-2020, 11:27 PM
Last Post: Heyjoe
  python library not defined in user defined function johnEmScott 2 3,885 May-30-2020, 04:14 AM
Last Post: DT2000
  Error: variable can not be defined julio2000 2 3,209 Feb-09-2020, 08:51 PM
Last Post: julio2000
  Variable defined but python wont recognize it. FWendeburg 3 3,332 Feb-19-2019, 10:43 PM
Last Post: woooee
  How do you make functions that take a variable that is not defined? magic 6 4,437 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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