indentation error inside of a loop?

This is the place for queries that don't fit in any of the other categories.

indentation error inside of a loop?

Postby hari vignesh » Sat Apr 05, 2014 1:38 pm

I am beginner ,I am using python 2.7.3 in GCC compiler while using if loop after the if loop if I try to write a code in next line and press enter it is showing error as
File "stdin", line 2
(displays line after if loop)
Indentationerror:expected an indented block
What does it means??
Last edited by Yoriz on Sun Apr 06, 2014 12:07 am, edited 2 times in total.
Reason: First post lock, Changed title.
hari vignesh
 
Posts: 1
Joined: Sat Apr 05, 2014 1:28 pm

Re: can't able to use if loop

Postby Mekire » Sat Apr 05, 2014 2:00 pm

Not sure what GCC has to do with python in this context (nothing would be my guess).
Next, there is no such thing as an "if loop"; while and for are loops.

Lastly and probably the problem here, whitespace is important in python.
If you have an if statement, the following lines that execute when that conditional is true need to be indented:
Code: Select all
if conditional:
    print("conditional is True")

Please post the exact code you are trying to execute and the exact error.
-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: indentation error inside of a loop?

Postby Seanliu » Sun Apr 06, 2014 11:12 am

You need to give us the code, so we (the wonderful community) can figure out the indentation. Also,

There is no such thing as an if loop.


a while loop is

Code: Select all
while num == 1:
    print(''Num is 1!")


and a for loop is

Code: Select all
for (i) in range (range):
    print(i)


there is no if loop. My point is, GIVE US THE CODE!
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: indentation error inside of a loop?

Postby micseydel » Mon Apr 07, 2014 2:41 am

Seanliu wrote:
Code: Select all
for (i) in range (range):
    print(i)

The first parens here are extraneous; though they're legal, they serve no purpose, and I recommend you not use them. The expression
Code: Select all
range (range)

is problematic as well. The built-in range() function takes an integer, not a function, which range() is, so I recommend you give better examples if that's what you want to do. The space is also extraneous and goes against existing coding standards (the reason is that the function and its arguments are coupled more tightly than things we tend to use spaces to indicate a lesser coupling of).
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1368
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: indentation error inside of a loop?

Postby Seanliu » Mon Apr 07, 2014 10:38 am

Try indenting (spacing) four spaces from the if.
Code: Select all
if a == b:
    print(a)


so that print(a) command is four spaces from the if. You can also put ifs inside ifs inside ifs and so on.


Code: Select all
if (a == b):
    if (a == c):
        if (b == c):
            print("A os equal to C")
else:
    pass()



EDIT: FIXED WEIRD INDENTATION
Last edited by Seanliu on Mon Apr 07, 2014 12:11 pm, edited 2 times in total.
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: indentation error inside of a loop?

Postby stranac » Mon Apr 07, 2014 11:39 am

Seanliu:

Yeah, that indentation makes no sense.
Also, pass is a statement, not a function.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: indentation error inside of a loop?

Postby micseydel » Mon Apr 07, 2014 3:57 pm

Seanliu: When you wish to give someone advice, please do so in one post rather than multiple. Many people see having a large number of posts as something earned, and making multiple posts on a single thread that would have been better as one artificial inflates that number. Also if you edit your post after someone comments on it, please, please, please explain why their post no longer makes sense.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1368
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests