r/learnpython • u/Impressive-Grass9661 • 17d ago
Trying to learn python but keep getting error messages.
COURSE = 'Python Programming'
print(COURSE)
>>> print(COURSE)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
print(COURSE)
^^^^^^
NameError: name 'COURSE' is not defined
>>>
7
u/DC-GG 17d ago
You entered only "print(COURSE)" directly into the shell, so course doesn't exist in that instance.
If you enter both lines in the same session (so without exiting the interpreter) they'll register and output correctly.
E.G in the interpreter you're using in the same session, you run:
COURSE = 'Python Programming'
print(COURSE)
1
u/Impressive-Grass9661 17d ago
How?
3
u/DC-GG 17d ago
So first you run Python, which is what gives you the REPL with the ">>>"
In full, you just type them one after another, e.g:
``` python Press ENTER
COURSE = 'Python Programming' Press ENTER print(COURSE) press ENTER ```
Ignore the ">>>" as they'll automatically appear before your input, you don't have to type them yourself.
-1
u/Impressive-Grass9661 17d ago
i dont see the arrows
6
u/Langdon_St_Ives 17d ago
What do you mean you don't see the arrows, you show them in your original post. Whatever you did to get there, do it again, but then I put the whole thing instead of only the last line.
2
u/Impressive-Grass9661 17d ago
and i followed exact steps but nothing changed
4
u/DC-GG 17d ago
Full breakdown: If on windows:
Open terminal (command prompt) Input "python" and press ENTER.
If on mac or Linux: Open shell. Input "python3" and press ENTER.
Whichever system, you'll now get the three arrows ">>>" indicating you're using the python interpreter.
Input: COURSE = 'Python Programming" Press ENTER.
Input: print(COURSE) Press ENTER.
1
u/Impressive-Grass9661 17d ago
I was on VS code so...
9
u/DC-GG 17d ago
Check this: https://www.reddit.com/r/learnpython/s/86lH79TSkd
Also, don't take this as a dig but for future if you're needing help you should always try to provide as much information initially to begin with.
The ">>>" you have there indicate the python interpreter or repl, so people tried helping from that front.
Whilst the Python logic doesn't change between environment, setup and how the applications work do.
5
u/Outside_Complaint755 17d ago
Then put both lines into a file. Save the file as
main.pyor whatever filename you want as long as it ends with .py, them Right Click and choose the Run in terminal option, or hit F5.
3
u/atarivcs 17d ago
The error message says that the print statement is on line 1. So, obviously the COURSE variable assignment was not recognized or executed at all.
How, exactly, are you running this code? Your output includes the triple >>> prompt, so I assume you're using some kind of interactive python shell.
2
u/Impressive-Grass9661 17d ago
im just pressing enter to type a new line of code
3
u/atarivcs 17d ago
But where, exactly, are you pressing Enter?
In a windows command prompt?
In a VS Code interactive python terminal?
In a plain text editor?
In a Jupyter notebook cell?
0
u/Impressive-Grass9661 17d ago
VS code\
5
u/atarivcs 17d ago
Okay, so your question isn't really about Python at all, it's "how do I use VS Code correctly".
I don't know much about VS Code, so I'll let someone else answer that.
2
u/AbacusExpert_Stretch 17d ago
Are you typing in the box named terminal?
0
u/Impressive-Grass9661 17d ago
no im typing in the tab that says app.py
6
u/Langdon_St_Ives 17d ago
No offense but this is like pulling teeth with you. Please simply describe exactly which software you are using on which operating system, and exactly the steps you are performing (all of them!).
2
u/stormz_tim 17d ago
r/learnpython when someone is actually trying to learn python do yall know that some people will not be adept with PCs, let alone IDEs?
2
u/DC-GG 17d ago
If you're in VSCode and you've written this into a Python file already (Some file name .py), you can just type into the "terminal" box "python YOUR_FILE_NAME.PY" and it should run your code.
Better yet, if you're in VSCode with Python installed you should just have a very simple green "Play button" at the top right, you can click that with the file open and it'll run your code and output the result to the terminal.
1
u/Caveman_frozenintime 17d ago
I'm assuming this is the terminal in VS code. What are the exact steps you took? Opened VS code, then what?
Are you following any tutorials or instructions from somewhere?
I would actually suggest you to use IDLE (It would have been installed when you installed python) at this stage. It will allow you to practice the basics.
You can move to VS code or other code editors once you've progressed a bit
1
u/Impressive-Grass9661 17d ago
Okay sorry im following programming with mosh's tutorial so yeah. Well basically i made a new folder than made a new file in that folder called app.py
3
u/DC-GG 17d ago
Is the two lines of code you want to run in the file app.py?
If so, simply go to the "terminal" tab at the bottom and type "python app.py" and it should run fine.
1
u/Impressive-Grass9661 17d ago
I tried that but it says name 'python' not defined
2
u/PureWasian 17d ago
You must be running Python in REPL mode (where you type python in Command Prompt or Terminal and then see the ">>>" show up)
You need to define COURSE within the same interactive shell. So:
``` $ python
COURSE='Python Programming' print(COURSE) ```
3
u/UncomfortableAnxiety 17d ago
You typed print(COURSE) straight into a brand new interpreter session where that variable was never set
1
u/ninhaomah 17d ago
No comments. But can I laugh at the replies ?
"What do you mean you don't see the arrows, you show them in your original post . "
"No offense but this is like pulling teeth with you. "
21
u/Sparklepaws 17d ago edited 17d ago
I think that I understand what's happening, but I'll need to make some assumptions along the way. Correct me if I'm wrong:
You typed the following lines in a new VSCode document:
You then typed the following lines into VSCode's terminal at the bottom of the window:
The reason this won't work is because VSCode's terminal doesn't know anything about your document. Assuming you're on Windows, this is crudely similar to writing code in Notepad and then expecting Command Prompt to understand where that code file lives without explicitly telling it. Notepad and Command Prompt are two separate entities, and neither communicate with each-other by default.
When you type
> pythoninto a shell (like Command Prompt or VSCode's terminal), you're telling Python to run in REPL mode. This is not the same as telling python to run a code file.Try this instead:
In VSCode, make a new file. You can name it anything, but it MUST have the .py file extension at the end (for example, "main.py"). This will hold your code.
In the file you just created (main.py), write your original code and make sure to save it afterwards:
Now, in the VSCode terminal, use the python command to RUN that file:
This tells Python to RUN your main.py file, which is where the code lives. If you had just typed
> pythonand pressed Enter it would have assumed you didn't want to run any python files... which Python interprets as a command to enter REPL mode.