Pyglatin 4/11


#1

Hello All,

Please can somebody explain why the commands “if len…” and “else” only work if they have not been indented and why the “Print statements” require an indentation.
Example of a command that does work below.

Thank you in advance.

Brendan

Blockquote

[print ‘Welcome to the Pig Latin Translator!’

Start coding here!

original = raw_input(“Enter a word:”)
if len(original) >5:
print original
else:
print “less than 5”]

Blockquote


#2

Any line that ends with a colon : tells the interpreter to expect a block, meaning the statement is not complete and there is a segment of code that must be executed given the correct condition.

if something:
    do something
else:
    do something else

The indentation is very critical in Python since there are no block containers like in other languages such as C, Java and JavaScript.

if (something) {
    do something 
} else {
    do something else
}

In the above we don’t need to indent since the block is clealy delineated by the curly braces. Python does not have those so the indents are absolutely necessary and must be consistent.


#3

thanks for your help MTF


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.