Python Forum

Full Version: I can't for the life of me get this basic If statement code to work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hiya,

I just started learning Python yesterday, and in one of the videos I was watching, the person showed a very basic piece of code involving two If statements. The issue is that I simply cannot get it to work in Python; I always get a syntax error after the fourth line, even though it looks EXACTLY how it does in the person's video.

>>> x = 5
>>> if x < 10:
... print('Smaller')
... if x > 20:
File "<stdin>", line 3
if x > 20:
^^
SyntaxError: invalid syntax

For clarity, after that four line which gives the error, the rest of the code is supposed to be:
... print('Bigger')

...print('Finis')

I'm sure it's some super basic thing I'm getting wrong here, but I've been trying to figure out what I'm doing for hours now and I'm clueless!

Cheers all
You normally don't type code like this in python. Normally you write code to a file and pass the filename as an argument to python.

myscript.py
x = 5
if x < 10:
    print("Smaller")  # Must indent + 1 level
if x > 20:    # Must indent -1 level
    print("Bigger")
print("finis")
And you run like this:
Output:
python myscript.py
(May-21-2024, 02:48 PM)deanhystad Wrote: [ -> ]The next line after an "if" must be indented one level. Not doing so is a syntax error.

You normally don't type code like this in python. Normally you write code to a file and pass the filename as an argument to python.

myscript.py
x = 5
if x < 10:
    print("Smaller")  # Must indent + 1 level
if x > 20:    # Must indent -1 level
    print("Bigger")
print("finis")

Hiya, thanks for the reply,

Sorry, I had indented after the ifs, but it didn't show up that way when I pasted it into here. Unfortunately, the issue is persisting though, I tried using your code in Python, and as the attached screenshot shows, exact same issue is still ocurring.
There are restrictions on code you can enter in the interpreter. You could enter your program like this:
[output]>>> x = 5
>>> if x < 10:
... print("smaller")
...
smaller
>>> if x > 20:
... print("bigger")
...
>>> print("finis")
finis
The python interpreter changes the prompt between ">>>" and "...". When the prompt is "...", python is telling you that it thinks you are inside an indented block of code, like the body of code following an "if" statement. Enter a blank line to exit the block and get the ">>>" prompt, then you can enter your second "if" statement. The interactive interpreter expects you to enter code one expression at a time.
alternatively you can have if/elif/else

>>> x = 25
>>> if x < 10:
...     print('smaller')
... elif x > 20:
...    print('huge')
... else:
...    print('bigger')
... 
huge
For the life of you? We don't want any deaths here!

The problem is the 2 ifs, 1 after the other I think.

I get:

x = 5
if x < 10:
    print("smaller")
    x += 10
if x > 10:
    print('bigger')
Output:
SyntaxError: invalid syntax
x= 5
if x < 10:
    print("smaller")
    x += 10
elif x > 10:
    print('bigger')
Output:
smaller
You need some elifs! (No, not elifants)

x = 1
# True is always True, the while loop will keep going unless you stop it!
while True: 
    if x < 10:
        print(f'This is if: {x} is smaller than 10')
        x += 1 # increase x by 1
    elif x == 10:
        print(f'This is first elif: {x} is less than 20')
        x += 2 # increase x by 2
    elif x > 10 and x <= 20:
        print(f'This is second elif: {x} is less than 20')
        x += 3 # increase x by 3
    else:
        print(f'This is else: {x} is {x}')
        break # stops the while loop
(May-21-2024, 02:56 PM)CandleType1a Wrote: [ -> ]
(May-21-2024, 02:48 PM)deanhystad Wrote: [ -> ]The next line after an "if" must be indented one level. Not doing so is a syntax error.

You normally don't type code like this in python. Normally you write code to a file and pass the filename as an argument to python.

myscript.py
x = 5
if x < 10:
    print("Smaller")  # Must indent + 1 level
if x > 20:    # Must indent -1 level
    print("Bigger")
print("finis")

Hiya, thanks for the reply,

Sorry, I had indented after the ifs, but it didn't show up that way when I pasted it into here. Unfortunately, the issue is persisting though, I tried using your code in Python, and as the attached screenshot shows, exact same issue is still ocurring.

So, am I not supposed to be typing directly into Python? I downloaded it and have been using it directly when working through several tutorials, but I've noticed that more than a few things I've been trying simply aren't working even when I'm using the same code shown on in tutorials. I just assumed I was supposed to be typing directly into the command prompt thing.
(May-21-2024, 03:30 PM)CandleType1a Wrote: [ -> ]So, am I not supposed to be typing directly into Python?
https://python-forum.io/thread-117.html
Thanks for the help guys. Think my main problem is me trying to get code to work directly within the Python command prompt. It was working fine on my first day because all the code I was trying was short and sweet, but everything completely fell apart today when I was trying slightly long and more complex programs. I'm using the Brackets program now and will run actual files within Python instead of directly writing code.

Thanks again all.