Python Forum
Variable not defined even though it is - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Variable not defined even though it is (/thread-41857.html)



Variable not defined even though it is - CoderMerv - Mar-28-2024

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?


RE: Variable not defined even though it is - menator01 - Mar-28-2024

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)


RE: Variable not defined even though it is - CoderMerv - Mar-28-2024

(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.


RE: Variable not defined even though it is - Larz60+ - Mar-28-2024

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.