6 Pop quiz!



<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
Not really stuck, just curious about something.

<What do you expect to happen instead?>
I wanted to add maybe an "elif"
my reason Is having three outcomes.
If the user didn’t enter a thing to print out “empty”.
if the user entered anything but letters to print "use letters only."
and if the user enter a word to print out he’s word.
how do i go about doing this?
I made a few switches around, in the elif i added the “isalpha()” deleting it from the if statement.
doesn’t work it still prints out the “empty…” I then switched around the “len()” to the “elif” and “isalpha()” to the “If” but it still the same results.

So in order for my idea to work how would i go about it to make it print three different outcomes?


print ‘Welcome to the Pig Latin Translator!’

original = raw_input("Enter a word: ")
if len(original) > 0 and original.isalpha():
print "well hello there " + original + "!"
print “empty, please use letters only.”

<do not remove the three backticks above>


but the pyglatin translator is not designed to handle more then a single word

if you want to do this you get (in pseudo code):

if isalpha
if length greater then zero
else print empty


This is one case where elif is not apprilicable since control flow is diverted on an OR path. Consider,

# let x = original

if len(x) > 0:
    print (x)
elif x.isalpha():
   print (x)
    print ("empty")

What if x is 987654? It will print. Only when len(x) is 0 will the elif even be encountered, and since x is the empty string, the default ‘empty’ prints.

Switching those two around and writing the x.isalpha() condition first will print ‘empty’ if the string has no length, If the string is numerals, then the len(x) condition will print x.

In both these cases we get false positives.

We must treat this as an AND scenario where both conditions must be met simultaneously. We could do it with two if statements,

if len(x) > 0:
    if x.isalpha():
        print (x)
    print ('empty')

Ideally, though, the logical expression pulls everything into on conditional expression, for simplicity.

if len(x) and x.isalpha(): print (x)
else: print ('empty')


But by using two “if” statements shouldn’t it have two “prints” as well?
It’ll still be two outcomes.

That’s new though, well to me, didn’t know we could use two “if” back to back without causing an error.


You observe correctly. What will it print when x is a numeral?


Well any character used is greater than 0 so empty haha

never mind, I re-wrote my code with two “if” adding a numeral, does nothing. no reply back.
that’s interesting.

Thank ya’ll!


Which means, as you suggest, a nested else and a print. You see how redundant that structure can get? Hence the logical expression to take away from the code bloat.


Psssh! I was just testing y’all! See if you knew haha

Yes, I can see how it gets redundant. Glad i asked, diving a little deeper into it helps me understand this syntax a bit better.

Thank you! (:slight_smile:


You’re welcome. The take away here, on top of the lesson, is,

if - elif - else       =>  OR

if - if - else - else  =>  AND


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