-3

I am a newbie in Python so I am trying to do some basic code. Below you can see a simple code, but it gives me subsequent error: File "", line 9 print('Not your number.') ^ IndentationError: expected an indented block

c = 0

while c < 5:
    
    print(f'The current number is: {c}')
    c = c + 1
    
    if c == 4:
    print('Not your number.')

else:
    print ('You have reached your limit.')

Could you please help me in finding the mistake? Thank you in advance!

Franta
  • 9
  • 8
  • 1
    Have you looked into the indentation requirements for an ‘if’ statement yet? – quamrana Jun 22 '20 at 20:43
  • 1
    Check out the official Python tutorial about control flow https://docs.python.org/3/tutorial/controlflow.html. Does your `if` statement look like those in the tutorial? How is it different? – ForceBru Jun 22 '20 at 20:45

2 Answers2

1

Just as you indent in your bottom else clause, you must indent for your if clause in your while loop. The below should work.

c = 0

while c < 5:
    
    print(f'The current number is: {c}')
    c = c + 1
    
    if c == 4:
        print('Not your number.')

else:
    print ('You have reached your limit.')
ziv
  • 99
  • 7
1

Indentation in python matters. When you have a statement which expects a block of code to execute after it like an if statement or a while loop if you don't provide a properly indented block you will get a syntax error.

c = 0

while c < 5:
    
    print(f'The current number is: {c}')
    c = c + 1
    
    if c == 4:
        print('Not your number.') # this statement has to be indented because of the if statement
else:
    print ('You have reached your limit.')
bhristov
  • 3,137
  • 2
  • 10
  • 26