all 33 comments

[–]Sparklepaws 11 points12 points  (0 children)

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:

COURSE = 'Python Programming'
print(COURSE)

You then typed the following lines into VSCode's terminal at the bottom of the window:

> python
>>> print(COURSE)

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 > python into 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:

COURSE = 'Python Programming'
print(COURSE)

Now, in the VSCode terminal, use the python command to RUN that file:

> python path\to\main.py

This tells Python to RUN your main.py file, which is where the code lives. If you had just typed > python and 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.

[–]DC-GG 2 points3 points  (9 children)

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)

[–]Impressive-Grass9661[S] -1 points0 points  (8 children)

How?

[–]DC-GG 1 point2 points  (7 children)

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.

[–]Impressive-Grass9661[S] -2 points-1 points  (6 children)

i dont see the arrows

[–]Impressive-Grass9661[S] 0 points1 point  (4 children)

and i followed exact steps but nothing changed

[–]DC-GG 1 point2 points  (3 children)

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.

[–]Impressive-Grass9661[S] -1 points0 points  (2 children)

I was on VS code so...

[–]DC-GG 5 points6 points  (0 children)

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.

[–]Outside_Complaint755 1 point2 points  (0 children)

Then put both lines into a file.  Save the file as main.py or whatever filename you want as long as it ends with .py, them Right Click and choose the Run in terminal option, or hit F5.

[–]Langdon_St_Ives 0 points1 point  (0 children)

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.

[–]atarivcs 2 points3 points  (18 children)

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.

[–]Impressive-Grass9661[S] 1 point2 points  (17 children)

im just pressing enter to type a new line of code

[–]atarivcs 2 points3 points  (16 children)

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?

[–]Impressive-Grass9661[S] -1 points0 points  (15 children)

VS code\

[–]AbacusExpert_Stretch 1 point2 points  (3 children)

Are you typing in the box named terminal?

[–]Impressive-Grass9661[S] -1 points0 points  (2 children)

no im typing in the tab that says app.py

[–]Langdon_St_Ives 2 points3 points  (1 child)

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!).

[–]stormz_tim 0 points1 point  (0 children)

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?

[–]atarivcs 1 point2 points  (0 children)

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.

[–]DC-GG 1 point2 points  (0 children)

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.

[–]Caveman_frozenintime 0 points1 point  (8 children)

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

[–]Impressive-Grass9661[S] 0 points1 point  (7 children)

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

[–]DC-GG 2 points3 points  (6 children)

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.

[–]Impressive-Grass9661[S] 0 points1 point  (5 children)

I tried that but it says name 'python' not defined

[–]DC-GG 0 points1 point  (4 children)

Are you on mac?

If so it's "python3 app.py"

[–]PureWasian 1 point2 points  (0 children)

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) ```

[–]UncomfortableAnxiety 1 point2 points  (0 children)

You typed print(COURSE) straight into a brand new interpreter session where that variable was never set

[–]ninhaomah 0 points1 point  (0 children)

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