0

codestyling and whitespaces are important in Python?

I have this simple code and works...

import cs50

while True:
    print ("Type a number between 0 and 23: ", end="")
    n = cs50.get_int()
    if n > 0 and n < 23:
        break

But if I delete the blanks spaces before break, doesn't work...

import cs50

while True:
    print ("Type a number between 0 and 23: ", end="")
    n = cs50.get_int()
    if n > 0 and n < 23:
break
~/workspace/pset6/ $ python mario.py
  File "mario.py", line 7
    break
    ^
IndentationError: expected an indented block

Whats is going on with the second code, guys??

0

Python doesn't use curly braces like C/C++ to group a block of code. Instead it uses indentation.
In C/C++

while(some expression){
//some code here
}

In Python

while some expression:
    #some code here

So indentation is the only way to tell Python interpreter that what follows is a block of code to be executed when that "some expression" evaluates to True.

| improve this answer | |
  • Thanks for the clarification Srikar – user13767 Jan 31 '17 at 17:12