Reputation: 31
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
Upvotes: 1
Views: 1559
Reputation: 14376
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
Upvotes: 0
Reputation: 8786
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"
Upvotes: 2