Why does this code work this way?


#1

This is the code that I wrote for the tutorial. It let me pass the section and it seems to be working as the computer thinks it should, but I'm a bit lost as to why it's doing what it's doing.

print 'Welcome to the Pig Latin Translator!'

raw_input("Enter a word:")
original = raw_input()

if len(original) > 0 and original.isalpha():
print original

else:
print "empty"


So the way I'm reading this code is like this:

It immediately welcomes me to using the translator. Immediately it asks me to type in any word I choose. That word is stored as the variable original. If the length of the word I wrote is more than 0 letters long and is made of only letters it should print the word that was entered. Otherwise it will print empty.

My issue is that no matter what word I type it prints "empty". Do I need to tell it to check the length of the word? I thought that was the point of the len(original) > 0. If anyone could shed light on this for me I would really appreciate it.


#2

Have you placed the right indention? Putting 4 extra spaces for each print statement after an if, elif or else statement is important for the computer to understand what you want to do.

Also as a recommendation, please provide next time the link to the specific exercise so members of this forum can help you better. It took some extra effort to find that this was Check Yourself. . .Some More


#3

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