Create palindrome: not seeing what is wrong with my code

https://app.dataquest.io/m/1012/python-fundamentals-practice-problems/3/palindrome

My code for this problem:

# provided inputs
input_1 = "TAAT"   
# answer to this input: True

input_2 = "AGTTA"  
# answer to this input: False

input_3 = "T" 
# answer to this input: True

def is_palindrome(input):
    reverse = ''
    for char in input[::-1]:
        reverse += char
    print (input, reverse) #only for debugging
    return (input == reverse)
    
    
print (is_palindrome(input_1))
print (is_palindrome(input_2))
print (is_palindrome(input_3))

What I expected to happen:

  • That it returns True, False, True
  • That my solution is accepted when clicking ‘Submit Answer’

What actually happened:

  • It does return True, False, True
  • And if I print ‘input’ and ‘reverse’ while executing the function, it really seems to work
  • When clicking ‘Submit Answer’, I get “Your code doesn’t seem to have the correct side-effects. Please re-check the instructions and your code.”

Did my code fail on one of the ‘hidden’ cases for this exercise?
Does anyone see what is wrong with my code?

Hi Jasper. It looks like the answer checker didn’t like using input as a variable. I got your code to pass by changing the input variable from input to dna.

Hi @jasperquak,

I was looking at your code the same time @april.g was (so I’ll let you know what I found too).

Your code looks good to me. I tried running it and it provided me with the output you expected: True, False, True (you can see if you are getting the expected results by clicking the run button rather than the submit button).

When I submitted it the code didn’t pass. I think the answer checker is getting tripped up with your debug line.

Try commenting out the following line in your is_palindrome function:

Print (input, reverse) #only for debugging

Good luck!

2 Likes

Thanks for the replies, @april.g and @bvalgard. To my surprise, commenting out the line with the print statement made a difference indeed. (Changing ‘input’ to ‘dna’ actually didn’t work for me.)

You’re right, I think I had deleted that line so I wouldn’t see the print statements and didn’t consider what impact that would have. :crazy_face:

1 Like
def is_palindrome(dna):
    if str(dna) == str(dna[::-1]):
        return True 
    else:
        return False

This seems more straight forward than using a for loop imo