I’ve replaced the A’s but I’m receiving an error about my output, what’s wrong?

Question

I’ve replaced the A’s but I’m receiving an error about my output, what’s wrong?

Answer

A very common issue here is forgetting to include the comma , at the end of the print statements inside the if and else. Without these, your output will be displayed on a new line! We want the output to all be on the same line.
Remember, Codecademy checks for exactly what it asks for, so if you’re missing a small detail like that, it will be marked wrong until it’s resolved.

3 Likes

Why is there an empty print statement at the end of the code?

1 Like

That would be to cancel the comma and move the print pencil to the next line.

9 Likes

Why do you have to put:

if char == "A" or char == "a" 

and not just simply:


if char == "A" or "a"
1 Like

will always be true since "a" is truthy. It is necessary to perform the comparison in both operands of the logical expression.

3 Likes

Apologies - not quite sure what you mean there. How can it always be true?

Any string of some length greater than zero will be truthy.

 False or True  => True
 False or 'a'   => 'a'

Let’s see it in action…

char = 'B'
if char == 'A' or 'a':
    print (True)
 else:
    print (False)

# True
1 Like

Shouldn’t this be false as per the ‘if’ statement and contents of the ‘char’ variable?

Same as with operators like + - * /, these are also operators carried out in some defined order and there is no connection there between 'a' and char, what you wrote does not cause them to be compared

what needs to happen for those to be compared is that == receives them both as arguments
you use == once but expect it to be used twice - doesn’t add up

Similarly, this does not cause 3 and 7 to be added. Why would it?
What is the behaviour of and? What have you been promised that it can do for you?

3 + 5 and 7  # 15?
3 Likes

it says 3 + 5 = 8, and “8 and 7” means that both 8 has to be true and 7 has to be true for the “and” to return true. However, it does not have anything to return to, as there is no “return” or anything like that command. Therefore, it returns 8 and 7, which 8 + 7 = 15. EXPLAINED.

Uh, not quite. Open your shell.

>>> 3 + 5
8
>>> 8 and 7
7
>>> 

AND is not about addition. It’s about truthiness. When the first operand is truthy, the second operand is returned, though as you say, this is not a program function, so one could prefer to use, yielded. Because there is no short-circuiting the expression defers to the second operand.

>>> 8 - 8 and 8
0
>>> 

AND yields to the first falsy it sees.

4 Likes

I’m confused by this error though the terminal outputs correctly?

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

# Add your for loop
for char in phrase:
    if char == 'A' or char == 'a':
        # Don't delete this print statement!
        print 'X',
    else:
        print char,

Hey I just got here, I might be late with the solution but… there is a “print” statement at the bottom of the code that we need there. So we have to have 3 “print” statements. Well Im not sure why but we need it. I hope we get some clarification for that later on.