Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

Help! IDLE keeps saying that 'else:' is a syntax error but I can't understand for the life of me WHY. password = 'password' currentattemp = 0 while currentattemp < 3: wrongpass = raw_input('Enter the password: ') if wrongpass != password: print('Try again') currentattemp += 1 else: print('Good Job!')

OpenStudy (anonymous):

I'm trying to say "IF wrongpass is NOT the password.... OTHERWISE/ELSE (if it IS the password) print 'good job' ". I don't get it...

OpenStudy (microbot):

if wrongpass != password: print('Try again') currentattemp += 1 <---- indent it or it thinks that else is not part of your if. else: print('Good Job!')

OpenStudy (anonymous):

I did that but now it says that first line with 'if' has incorrect indentation

OpenStudy (anonymous):

OpenStudy (microbot):

if wrongpass != password: ---------print('Try again') currentattemp += 1 else: print('Good Job!') see how much indented they are?count the -....i think python counts 4spaces as legal indentation.... if wrongpass != password: print('Try again') currentattemp += 1 else: print('Good Job!') check all of the indentations in your code aswell...let me know if it worked?

OpenStudy (microbot):

hhhm nvm that...still getting this error...looking into it

OpenStudy (microbot):

ok do as the error message says! undent everything and then indent everything to its right place. select code -->Format-->Undent/Indent....it fixed it for me...it was just messed up.!:)

OpenStudy (anonymous):

Did you manage to get this working?

OpenStudy (anonymous):

try typing all that again(correctly)....... and for the spaces below " if " it will automatically go to the right place after hitting enter

OpenStudy (anonymous):

It works for me: then you could change 'break' for whatever you want your program to do when the correct password is written

OpenStudy (anonymous):

Python indentation is picky, but pretty straightforward. There are two basic rules: 1. The indented block is preceded by a line that ends with a colon (note: there are only a few Python keywords that use this mechanism, such as 'if', 'else', 'elif', 'while', 'def' (for functions) and 'class'). 2. Every line in the indented block must have the same indenting as every other line in the block (e.g. if one line has a single space before the first character of code, then every line in the block must have a single space before the first character of code). Other than that, the rules are pretty open. In fact, as long as every block is self-consistent, every block different from each other if you like, though it's *extremely* bad practice and you won't thank yourself if you try it. This, for instance, is legal (though I must point out again that it's *very* bad practice!): if (i = 1): j = j + 1 if j > 10: k = k + 1 l = l * 2 else: j = 0

OpenStudy (microbot):

@Curiousmind Great answer!ty @ianbelcher 's problem was that his python (or him) messed slightly with indentation(no idea where (apart the currentattempt line)) and gave him that error he linked. I tried it on my machine and it gave me the same error....then by doing what the error message told i resolved the problem and it worked fine! So practicaly what he has to do is Undent everything and then Indent again by using the format command not manually.

OpenStudy (anonymous):

use either all spaces or all tabs for indentation, not a mixture of both. some editors will give you a tab when doing 'auto indent' then when you use some spaces during editing, you can get these warnings.

OpenStudy (rsmith6559):

I've given up using IDLE because it changes my tabs to spaces and creates errors like that.

OpenStudy (anonymous):

I did the same thing to get @ianbelcher's code to work. I "dedented" everything and then indented again and it worked.

OpenStudy (anonymous):

spaces, spaces, spaces -

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!