Could someone give me an explanation about why this Palindromas words reoccurrence software does not work in Python?
def palindromo(cadena):
num_letras = len(cadena) - 1 # te da el numero da caracteres -1
for i in range(len(cadena) // 2): # repite el loop 2 veces
if cadena[i] != cadena[num_letras]: # si la pimera y ultima letra != entoces la funcion es falsa(0!=3)
# si la segunda y la ultima letra != la funcion es falsa (1 != 3)
return False #si se cumple la condicion enntonces la funcion es falsa
else: #de lo contrario:
num_letras = num_letras - 1 #le restas 1 a num_letras(por que?)
return True #esto devuelve toda la funcion como verdadera
palabra = "abba"
if palindromo(palabra):
print ("es palindromo")
else:
print ("no es palindromo")
This is the line (fourth line) that I do not fully understand:
if cadena[i] != cadena[num_letras]:
If I'm not mistaken, this tells me that it reviews 0! = 3 and 1! = 3 right? Then why does not it work ... * My specific question is if the only thing the if does is this * for example:
word = abba:
a! = a, b! = a
if the condition is met the word is not palindrome