If you have any questions, comments or issues with this project please post them here!
i stripped and lowered the input but it’s still showing " A Man A Plan A Canal Panama" is not a palindrome can someone tell me what am I doing wrong here
def palindrome(word):
if len(word)<=0:
return “It’s a Palindrome🎊”
if word[0] != word[-1]:
return “booo its not a palindrome!”
return palindrome(word[1:-1])
print("palindrome finder ")
type=input("please type a word to find if its palindrome or not \n~ ").strip().lower()
print()
print(palindrome(type))
Hi Rocko,
Looks like you need to remove the spaces when taking input. You can do this by using the .replace()
function:
def palindrome(word):
if len(word) <= 0:
return "It's a palindrome!"
if word[0] != word[-1]:
return "boo, it's not a palindrome."
return palindrome(word[1:-1])
print("palindrome finder")
word = input("please type a word to find out if it's a palindrome or not\n > ").strip().lower().replace(" ", "") # Replace any spaces with nothing
print("\n" + palindrome(word))
oO, what’s the point of the strip() doesn’t it do the same as removing spaces whats the difference between these two can you tell plz? anyways thanks for the quick reply! much appreciated
I believe .strip()
only removes whitespace at the end of what you input, for example:
name = input("What's your name?\n").strip() # Taking user input and stripping it
Now if you were to enter something with trailing whitespace (ex: "Rocko ") the extra space at the end would get cut off.
Hope this helped!
@QwertyQwerty88,
oOh neat thnx and sorry for the late reply fell asleep and then forgot about it totally lol … anyways happy new year!
Happy new year! (Posts must be 20+ characters lol)
hahaha cheers bro enjoy
I think i’ve done it wrong but it works :))
Sooo … it’s not stupid if it works … until it’s not …
while True:
word = input("Write a word: ").lower()
wordrev = word[::-1]
if word == wordrev:
print(f"{word.capitalize()} is a palindrome!")
else:
print(f"{word.capitalize()} is not a palindrome!")
yes … you can stupid proof it … and so on … but it does what its supposed to do … corect ?
Just FYI, if you want to save yourself some typing, .title()
capitalizes the first letter of your word and .upper()
makes the whole thing capital. There’s nothing wrong with .capitalize()
, I just prefer shorter phrases.
Thank Elijah.
Laziness is the first clue that gives away a coder :)))
BTW:
How do you post snippets of code nicely on the forum. I see your functions are nicely printed too …
This works on Ask, Replit, and .md
files.
Use the backtick three times to start code on a line, then type code on line(s), then three more backticks on another line. You can specify the language by typing it (no space) after the first three backticks (e.g., Python
). The backtick is the same thing you use for single line code snippets. It’s to the left of the 1 on an American keyboard. IDK about other keyboard layouts.
EDIT: For indents, you have to type the spaces yourself.
This is my best with recursion!
def recursion(value1, value2):
if len(llista) == 1 or len(llista) == 0:
return
elif llista[value1] == llista[value2]:
llista.pop(value1)
llista.pop(value2)
recursion(value1, value2)
else:
return
paraula = input("escriu una paraula: ")
paraula = paraula.lower()
if " " in paraula:
paraula = paraula.strip()
llista = []
for i in paraula:
i.split()
llista.append(i)
recursion(0, -1)
if len(llista) == 1 or len(llista) == 0:
print("es un palindrom")
else:
print("no es un palindrom")
In the hints it was said to remove the letters once compared
So I’ve put them into a list, and then pop them!
ah boy. a minute into the solution video and I see that my working solution was not brainy enough.
while True:
word = input("Type a word > ").strip().lower()
if word == word[::-1]:
print(f"{word} is a palindrome! True!")
else:
print(f"{word} not a palindrome. False.")```
What do you mean by this?
once I saw David pull out the len()
parameter in his solution (which I first thought about, but couldn’t figure out how to incorporate it), I knew I was taking a shorter route to the solution.
plus, there’s no recursion in my solution. This was a tough challenge for me.