1

Instructions: Write an if statement that verifies that the string has characters.

Add an if statement that checks that len(original) is greater than zero. Don't forget the : at the end of the if statement! If the string actually has some characters in it, print the user's word. Otherwise (i.e. an else: statement), please print "empty". You'll want to run your code multiple times, testing an empty string and a string with characters. When you're confident your code works, continue to the next exercise.

print 'Welcome to the Pig Latin Translator!' # Start coding here! if len(original) > 0: return True else len(original) <= 0 return False original = raw_input("Enter a word:") print original print "empty" 

I'm stuck as I keep getting the following error. What am I doing wrong?

File "python", line 6 else len(original) = 0 ^ SyntaxError: invalid syntax 

2 Answers 2

2

You can not have a check condition on an else

if len(original) > 0: return True else: return False 

So your full answer should look something like (Based upon the description of what it wants you to do that you provided, NOT the direction you were going in):

original = raw_input("Enter a word:") if len(original) > 0: print original else: print "empty" 
Sign up to request clarification or add additional context in comments.

Comments

0

if this is your code, it won't work at all because it will return before it does anything
your print original should replace the return True, and print empty should replace return False

After removing everything after else on that line of course (and including the :)

print 'Welcome to the Pig Latin Translator!' original = raw_input("Enter a word:") # Start coding here! if len(original) > 0: print original else: print 'empty' 

-- Or --

print 'Welcome to the Pig Latin Translator!' original = raw_input("Enter a word:") result = original if len(original) > 0 else 'empty' print result 

5 Comments

@KevinDTimm lets not scare him now...he is just beginning :p
Sorry, I can't help myself when I see these opportunities to introduce the ternary operator!
haha @heinst i was stuck looking at that ternary operator like "huh"
That's not valid syntax in Python. The correct ternary operator is original if len(original) > 0 else "empty".
@Navith - almost but not quite. I have fixed it in the post. <editorial>Unfortunately this 'ruins' the ternary operator in that it doesn't really jump out at you when you see it.</editorial>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.