Python Forum
I can't for the life of me get this basic If statement code to work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I can't for the life of me get this basic If statement code to work
#1
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

Attached Files

Thumbnail(s)
   
Reply
#2
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
Reply
#3
(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.

Attached Files

Thumbnail(s)
   
Reply
#4
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.
Reply
#5
alternatively you can have if/elif/else

>>> x = 25
>>> if x < 10:
...     print('smaller')
... elif x > 20:
...    print('huge')
... else:
...    print('bigger')
... 
huge
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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
Reply
#7
(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.
Reply
#8
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information Is it possible to multi line a Basic Function Construct line statement? If so how? BrandonKastning 7 382 May-23-2024, 03:02 PM
Last Post: deanhystad
  hi need help to make this code work correctly atulkul1985 5 932 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 789 Oct-22-2023, 09:08 PM
Last Post: tronic72
Photo Python code: While loop with if statement HAMOUDA 1 676 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 801 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Beginner: Code not work when longer list raiviscoding 2 922 May-19-2023, 11:19 AM
Last Post: deanhystad
  code won't advance to next statement MCL169 2 815 Apr-11-2023, 09:44 PM
Last Post: Larz60+
  Why doesn't this code work? What is wrong with path? Melcu54 7 2,001 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,587 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 988 Dec-29-2022, 08:59 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020