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
#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


Messages In This Thread
RE: I can't for the life of me get this basic If statement code to work - by Pedroski55 - May-21-2024, 03:22 PM

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 531 May-23-2024, 03:02 PM
Last Post: deanhystad
  hi need help to make this code work correctly atulkul1985 5 1,046 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 855 Oct-22-2023, 09:08 PM
Last Post: tronic72
Photo Python code: While loop with if statement HAMOUDA 1 712 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 870 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Beginner: Code not work when longer list raiviscoding 2 986 May-19-2023, 11:19 AM
Last Post: deanhystad
  code won't advance to next statement MCL169 2 851 Apr-11-2023, 09:44 PM
Last Post: Larz60+
  Why doesn't this code work? What is wrong with path? Melcu54 7 2,135 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,672 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 1,047 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