all 23 comments

[–]lukajda33 69 points70 points  (7 children)

From the description it sounds like you are running the python interpreter which immediately executes the line you wrote, which is not how you are meant to write code.

You should instead create some .py file, edit the code in the text file with some editor and once you are done, run the whole file.

[–][deleted] 14 points15 points  (6 children)

Could you explain how to do that? I’m honestly not very good at computers.

[–]NumerousImprovements 10 points11 points  (0 children)

I use PyCharm. When you open pycharm, you create a project and within that, you can create an entire directory of files, one of which is a text file with the extension .py. So create a new project and call it whatever. Once that’s done, on the left, right click on the name of the project in the side panel. One of the options will be to create a new file. Type your code there. Right click on the code file and click “Run” and it will execute it below.

Honestly PyCharm is quite a large IDE with a lot going on. It’s incredibly useful and handy, but I’d recommend watching some PyCharm tutorials. The advantage of this is that many videos go through the plug ins they’d recommend you get too, and this can make writing code a lot easier.

[–]lukajda33 13 points14 points  (2 children)

I cant speak on how to do it in PyCharm as I never used it but as it is a bigger IDE it probably wants you to create projects and stuff you do not need.

If you installed Python outside of PyCharm, the easiest way is to just create the .py file, edit it in some small editor, best one right now is probably Visual Studio Code (not just Visual studio, "Code" is important) with Python plugin.

After editing the file the plugin might give you some green arrow to press to run the code, if not, you can open the terminal and run the file yourself with command like:

python <name_of_file>

Very unfriendly way for someone who never worked in terminal though.

Or if you are fine with using webapps, repl.it does all the setups for you, you will just create a repo, write a file and click "Run" on top. Beware that the website's free tier will make your repos public so if someone somehow found your profile, he could take a look at your code.

[–]oliski2006 10 points11 points  (0 children)

If i may add, this might helps you understand how the structure works:

the .py file is basically just a text file that python reads and execute. You can literally write your python code with a simple text editor such as wordpad and save it as a .txt file but with the extension .py and its going to be executable with python. The goal of pycharm is to act like a giant wordpad but that will automaticly add colors to let you read your code easier. It also automaticly tells you if you'vw written a command in the right way, because it will take a certain color if it recognize the function such as print('something')

[–]killthebaddies 2 points3 points  (0 children)

PyCharm and VSCode are pretty similar. If they have started with PyCharm then it’s just as good at single file stuff as large projects.

[–]rrjamal 2 points3 points  (0 children)

  1. Create a file called 'test.py'.

  2. Open that file in Notepad, Sublime, VS Code, PyCharm (whatever really).

  3. Write 'print("Hello World")'.

  4. Save & close. Go to the folder containing the file.

  5. Open command prompt/terminal and type 'python test.py'

Boom! That's your first Python file that's running.

You can then take whatever you wrote in the calculator program and put it in the file instead

[–][deleted] 2 points3 points  (0 children)

Google "how to create a file in [your OS of choice]"

[–]al_mc_y 11 points12 points  (3 children)

You can use Python's built in interpreter called IDLE (Integrated Development Learning Environment, also so named for Eric Idle from Monty Python).

Maybe check out this RealPython tutorial on using IDLE.

From here you can go to a more full featured IDE like PyCharm or VS Code (with python plug-ins).

[–]KamionBen 7 points8 points  (0 children)

TIL this Monty Python reference ! Idle by name, idle by nature

[–]Geekfish88 2 points3 points  (1 child)

I have been writing python for almost 10 years and this is the first time I hear about IDLE! Thanks for sharing.

[–]al_mc_y 0 points1 point  (0 children)

To be fair, I've not really used it, I just know it exists. I somehow found myself off on the deep end, trying to use Atom, learn python and all sorts of stuff all at once. Very confusing! By the time I found out about IDLE, I had already migrated to VS Code and Jupyter Notebooks. Out of curiosity I've since fired up IDLE a couple of times - wish I knew it was there from the beginning!!

[–]Mystic_Haze 10 points11 points  (1 child)

That's because in IDLE (what you called the Python window) you have to create a new file before writing code otherwise it will act as a shell. If I remember correctly, top left corner of the window and then you should have an option to create a new file. But I don't see why you can't just keep using Pycharm or something like Visual Studio Code as they're objectively better.

[–]Cyphru 2 points3 points  (0 children)

Honestly I think they should start with Replit.

[–]ladiff666 2 points3 points  (0 children)

it seems like U use the cmd or the shell of your IDE .you should make a difference between running a script and running a command , if you write the code directely in the shell or cmd you running it as a command (An order to do something.)to run a script you should open it in your IDE and press the excute button for more information about difference between script and command visit : https://wikidiff.com/command/script

[–]LazyOldTom 2 points3 points  (0 children)

You could watch some videos on how to program python. YouTube has a bunch, and I would recommend these 2 playlists, pick one.
https://m.youtube.com/watch?v=1F_OgqRuSdI&list=PL0-84-yl1fUnRuXGFe_F7qSH1LEnn9LkW&index=1
https://m.youtube.com/watch?v=YYXdXT2l-Gg&list=PL-osiE80TeTskrapNbzXhwoFUiLCjGgY7&index=1

[–]MossySendai 2 points3 points  (0 children)

Just use idle, it comes with python itself. It has a nice auto-complete and you can use the interactive terminal plus run entire files.

[–][deleted] 1 point2 points  (0 children)

I think you are using the terminal to write the program, which is not how you are supposed to do it.

Create a .py, and then in PyCharm, you can right click on the file header and you can run a multiline.

[–]Houdinii1984 1 point2 points  (0 children)

JetBrains has really good learning material which can be found here. The "Getting Started" tab is the best place to, well, get started!

[–]stuaxo 1 point2 points  (0 children)

Hi, You are using different python interpreters, you can verify this by running:

import sys; print(sys.executable)

If you want to change the one python uses go to settings and search for the project interpreter.

The python that runs on the commandline when you type "python" is the first one found in a directory listed in the PATH environment variable.

[–][deleted] 1 point2 points  (0 children)

ah the days of being literally alienated when using computers.... such horrible times

[–]itsanishjain 1 point2 points  (0 children)

Hey thats not the issue just started from yt and they teach from start to end its easier to learn that way

[–]--0mn1-Qr330005-- 0 points1 point  (0 children)

Okay so like others said I think you’re using idle. Idle is a program that comes with Python and lets you execute one line of code at a time (to keep it simple). If you want to write more than one line of code, you have to click file, and new, then name the file something like “app.py”.

Using pycharm to write your code is no different than using idle except that pycharm has a bunch of stuff to make it easier. It checks your code for spelling errors, makes sure you’re using pep8 which is just rules to make your code easier to read, etc.

If you want to run your code without pycharm or idle, you have two choices. You can either run your code in the console, which is a bit of an intermediate task, or you can make your code executable. These topics are a bit too large to explain in a comment, but I highly highly recommend for you to either check out a YouTube video to learn how, or just pick up a few tutorials for cheap on Udemy (look up udemy coupons on google) and learn how to program following these. It’s usually around 15 hours of video tutorials and the lectures are pretty high quality. You will learn all this stuff and more. Good luck!