Python 8. Loops 'For your A'


why my console shows for my codes at the very bottom

Oops, try again. Did you replace all of the "A"s and "a"s with "X"s? It looks like you printed "X X X X X X X X X X X X X X X X X X X X X" instead of "X b i r d i n t h e h X n d . . .".

how come this code below
if char == 'A' or 'a':
doesn't work? when
if char == 'A' or char == 'a'

phrase = "A bird in the hand..."

# Add your for loop
for char in phrase:
    if char == 'A' or 'a':
        print 'X',
#    else:
#        print char,

#Don't delete this print statement!


Will always yield True. 'a' is a string with length greater than zero, so truthy

Will only yield True when char is 'A' or 'a'.


Oh that's why it showed me the result with true for all letters.
Thank so much mtf!!


And and Or are used to link comparisons and not the values inside those comparisons.


When the expression involves two comparisons, yes, both need to be written as comparisons. AND and OR work with individual values, as well. Consider above we spoke about truthy and falsy...

0, "", '', None                    =>  all falsy

-1, 1, Infinity, "A", "a",  &c.    => all truthy

The types don't have to be the same, either:

"A" or 0        => True  "A", actually

"" or 1         => True   1, actually

3.14 and None   => False  None, actually

3.14 and "PI"   => True   "PI", actually

This will make more sense as you progress.


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