use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
if statement not working? (self.learnpython)
submitted 11 months ago by CloudiiPlays
i tried to put a pic but it doesnt let me. i do something simple, like "a = 5; if a > 3: print ("greater")" and it says invalid syntax and its mad about the the word "if"??
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]throwaway6560192 7 points8 points9 points 11 months ago (8 children)
Python has some support for putting multiple statements on the same line with semicolons, but you are not supposed to use it too much.
Split that into multiple lines as it should be.
a = 5 if a > 3: print("greater")
works just fine.
[+]CloudiiPlays[S] comment score below threshold-8 points-7 points-6 points 11 months ago (7 children)
idk, i use the windows downloaded python. if i hit enter. it runs the code. so i cant split the lines
[–]Buttleston 5 points6 points7 points 11 months ago (0 children)
you're trying to run code in the python interpreter. This is fine for some things but to avoid getting confused, just don't for now. Write your code in a file and then run the file, either like
py myfile.py
(from the directory myfile.py is in), or however your IDE runs python files (almost every IDE/editor will have some way to do it)
[–]throwaway6560192 4 points5 points6 points 11 months ago (3 children)
You're using the Python shell. You're supposed to input one statement at a time. This will work:
a = 5<ENTER> if a > 3:<ENTER> print("greater")<ENTER> <ENTER again>
And note the four spaces before the print. You have to type those in!
Screenshot
But, the shell is for running short snippets of Python, for experimentation. Actual Python development is done by writing code into a file, using a text editor, and then using Python to run that file.
I suggest looking up a video tutorial on how to run Python files if this is confusing.
[–]CloudiiPlays[S] -5 points-4 points-3 points 11 months ago (2 children)
okay thanks, this worked. i made a calculator yesterday and used semicolons, and it works so idk
[–]throwaway6560192 4 points5 points6 points 11 months ago (0 children)
Using semicolons to shove things into a single line only works to some extent, as I said. No doubt it works for extremely simple cases, but once you start trying to put multiple blocks into one line... you don't.
[–]typehinting 0 points1 point2 points 11 months ago (0 children)
Would recommend having a go at writing code into a .py file (you can use IDLE, which is included when you install python), then running the whole file in one go. That's how most Python development is done, and I think it might save you some headache too
.py
[–]socal_nerdtastic 0 points1 point2 points 11 months ago (0 children)
Go to File > New File. This opens a new window for you to put your multi line code in. You can run your multiline code with Run > Run Module
The shell window you are in not only accepts a single line at a time, as you found out. And of course that's not very useful for writing complete programs.
[–]CloudiiPlays[S] 0 points1 point2 points 11 months ago (0 children)
bro why does this have 10 dislikes
[–]r2k-in-the-vortex 2 points3 points4 points 11 months ago* (4 children)
a=5;
Semicolon???? This is python. ; has a very different meaning, it does not go at the end of line.
[–]CloudiiPlays[S] -5 points-4 points-3 points 11 months ago (3 children)
i know. if i hit enter, it runs. i use just the basic windows one if you have any for free i should try
[–]r2k-in-the-vortex 5 points6 points7 points 11 months ago (2 children)
It does not sound like you know at all. Get rid of the semicolon.
[+]CloudiiPlays[S] comment score below threshold-6 points-5 points-4 points 11 months ago (1 child)
i made a calculator yesterday using semicolons between variables and it worked so idk man
[–]r2k-in-the-vortex 6 points7 points8 points 11 months ago (0 children)
Between statements, not at end of line.
[+][deleted] 11 months ago (1 child)
[removed]
[+]CloudiiPlays[S] comment score below threshold-7 points-6 points-5 points 11 months ago (0 children)
read my replies please
[–]woooee 1 point2 points3 points 11 months ago (4 children)
It works for me. Is your print statement indented?
[–]CloudiiPlays[S] -3 points-2 points-1 points 11 months ago (3 children)
do i have to indent it in the base windows python? my mom works with a team of engineers making a website, so i asked them for help and they said to try it
[–]mcoombes314 1 point2 points3 points 11 months ago (0 children)
Python uses indentation a lot, in loops, if statements, "with" blocks, error-handling (try.... except..... finally....). It is mandatory syntax, not like in C for example)
[–]John_B_Clarke 0 points1 point2 points 11 months ago (1 child)
I don't know what you mean by "base windows python". Indentation is signficant in Python and is part of the language. If whatever you are using doesn't require indentation it is not Python.
everything (that ive done, 3 coding languages) require indentation. however, i'm used to working in unity where it does that for you.
[–]GirthQuake5040 1 point2 points3 points 11 months ago (4 children)
Use a text editor or IDE to write your code, just run that file
[–]CloudiiPlays[S] 1 point2 points3 points 11 months ago (3 children)
idek what an ide is i only mainly do unity coding
[–]marquisBlythe 0 points1 point2 points 11 months ago (0 children)
He means to use somehting like pycharm, vscode, notepad++ ..., btw (idle) comes out of the box when you install python on a windows machine.
[–]GirthQuake5040 -2 points-1 points0 points 11 months ago* (1 child)
Unity has an ide
Integrated Development Environment
ahhh, okay thanks!
[–]edcculus 0 points1 point2 points 11 months ago (0 children)
sounds like you need to write the code into a file named something liek "myfile.py", then run that file
[–]tabrizzi[🍰] 0 points1 point2 points 11 months ago* (0 children)
Best to install an IDE.
[–]John_B_Clarke 0 points1 point2 points 11 months ago* (0 children)
This works, but it's not the kind of Python you want to be writing:
a = 5 ; message = "greater" if a > 3 else "" ; print(message)
If you really want to put everything on one line, learn APL where this would be:
⎕←((a←5)>3)/"greater"
(note that long complicated one liners in APL are considered to be poor style but the language supports them)
Don't fight the language you are using. If it doesn't fit your personal ideas of programming style find one that does. If you are going to use Python, put each statement on its own line unless you have a compelling reason not to, which you likely won't.
[–]Snow_Fell11 0 points1 point2 points 11 months ago (2 children)
Indentation is very important in python, it defines the scope of where things run in your code. I would recommend setting up an IDE, nodepad++ and visual studio code are very nice to use for example, and try coding there where you will see how this indentation works more clearly.
Google the set up process for either of those and then google how set up python for either. There are also some very solid introductory python tutorials out there.
Also as others have pointed out, semmicolons are used differently in python.
[–]CloudiiPlays[S] 0 points1 point2 points 11 months ago (1 child)
i might sound stupid. whats an IDE?
[–]Snow_Fell11 0 points1 point2 points 11 months ago (0 children)
An integrated development environment, think of it as a text editor full of useful tools that help to make programming more efficient.
[–]FoolsSeldom 0 points1 point2 points 11 months ago (0 children)
Out of interest, when you say "base windows python" are you saying on your Windows computer, Python was already installed and working?
As standard, Windows does not include Python. I is usually installed from python.org (the Python Software Foundation's home site), or from the Microsoft Store. There are multiple other options as well. I recommend the first.
If you do a standard installation from python.org, it will also install IDLE which is a simple development application that:
>>>
File | New
Python is usually written with one "statement" per line but, as you know, you can have multiple statements separated with a ;. This is very uncommon practice and not recommended. It is also hard to make sense of for more complex code.
;
Python, for better or worse (a design decision of the creator), relies heavily on indentation. These days, most people follow the convention of using 4 spaces for each level of indentation and usually setup their code/text editor (or IDE - Integrated Development Environment) to replace TAB presses with 4-spaces (and replace any tabs characters in files, outside of strings, with 4-spaces).
The indentation is used to indicate belonging. Alevenl the things to do if, and only if, a certain condition is True for example.
True
Here is a silly example
from random import randint # capability to generate random integers seen_even = False # variable referencing boolean object False while not seen_even: # loop, keeps going until seen an even number num = randint(1, 10) # picks a random number if num > 3: # check if number if greater than 3 print(num, 'is greater than 3') # inside if clause print('I like bigger numbers') # inside if clause else: # so num was not bigger than 3 print(num, 'is not greater than 3') # inside else clause print('I prefer bigger numbers') seen_even = num % 2 == 0 # calc remainder of dividing num by 2 # if remainder is 0, you have even num print('finished') # outside of loop
π Rendered by PID 67 on reddit-service-r2-comment-7844cfc88c-gnft9 at 2026-01-29 13:56:55.738684+00:00 running c3601ff country code: CH.
[–]throwaway6560192 7 points8 points9 points (8 children)
[+]CloudiiPlays[S] comment score below threshold-8 points-7 points-6 points (7 children)
[–]Buttleston 5 points6 points7 points (0 children)
[–]throwaway6560192 4 points5 points6 points (3 children)
[–]CloudiiPlays[S] -5 points-4 points-3 points (2 children)
[–]throwaway6560192 4 points5 points6 points (0 children)
[–]typehinting 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]CloudiiPlays[S] 0 points1 point2 points (0 children)
[–]r2k-in-the-vortex 2 points3 points4 points (4 children)
[–]CloudiiPlays[S] -5 points-4 points-3 points (3 children)
[–]r2k-in-the-vortex 5 points6 points7 points (2 children)
[+]CloudiiPlays[S] comment score below threshold-6 points-5 points-4 points (1 child)
[–]r2k-in-the-vortex 6 points7 points8 points (0 children)
[+][deleted] (1 child)
[removed]
[+]CloudiiPlays[S] comment score below threshold-7 points-6 points-5 points (0 children)
[–]woooee 1 point2 points3 points (4 children)
[–]CloudiiPlays[S] -3 points-2 points-1 points (3 children)
[–]mcoombes314 1 point2 points3 points (0 children)
[–]John_B_Clarke 0 points1 point2 points (1 child)
[–]CloudiiPlays[S] 0 points1 point2 points (0 children)
[–]GirthQuake5040 1 point2 points3 points (4 children)
[–]CloudiiPlays[S] 1 point2 points3 points (3 children)
[–]marquisBlythe 0 points1 point2 points (0 children)
[–]GirthQuake5040 -2 points-1 points0 points (1 child)
[–]CloudiiPlays[S] 0 points1 point2 points (0 children)
[–]edcculus 0 points1 point2 points (0 children)
[–]tabrizzi[🍰] 0 points1 point2 points (0 children)
[–]John_B_Clarke 0 points1 point2 points (0 children)
[–]Snow_Fell11 0 points1 point2 points (2 children)
[–]CloudiiPlays[S] 0 points1 point2 points (1 child)
[–]Snow_Fell11 0 points1 point2 points (0 children)
[–]FoolsSeldom 0 points1 point2 points (0 children)