如何检查单词是否为回文(Python)

如何检查单词是否为回文(Python),python,Python,请帮忙。。。 因此,指令要求对计算机进行编程,以检查单词是否为回文。我输入了这个代码: def is_palindrome(word): counter_from_first_letter=0 counter_from_last_letter=-1 from_first_letter = word[counter_from_first_letter] from_last_letter = word[counter_from_last_letter] max_index_from_first

请帮忙。。。 因此,指令要求对计算机进行编程,以检查单词是否为回文。我输入了这个代码:

def is_palindrome(word):

counter_from_first_letter=0
counter_from_last_letter=-1

from_first_letter = word[counter_from_first_letter]
from_last_letter = word[counter_from_last_letter]

max_index_from_first= len(word)
max_index_from_last= (len(word))*-1

while from_first_letter == from_last_letter:  
    from_first_letter = word[counter_from_first_letter]
    from_last_letter = word[counter_from_last_letter]

    counter_from_first_letter += 1
    counter_from_last_letter -= 1
    return True

问题是,计算机只会检查首字母和末字母是否相同,如果相同,它只会返回true。我如何确保电脑检查每一封信?谢谢

也许是这样的:

def is_palindrome(word):
    if word == word[::-1]:
        return True
    else:
        return False

也许您可以尝试以下方法:首先将字符串转换为列表,然后反转列表并将其转换回字符串。比较两个字符串是否匹配?它们是回文,如果不是,它们就不是

'''checking whether a word is a palindrome
we first convert the word into a list then join it;
use an if statement to compare the two strings'''

def palindrome(string):#you need the input(string)
    palindrome_check=[]#create an empty list
    for character in string [::-1]:#create a list from the input
        #(use a for loop because you now know the range)
        #the [::-1] analyzes the characters in reverse 
        palindrome_check.append(character)#add each character to the new empty list
    #print(palindrome_check)
    rev_string= ''.join(palindrome_check)#.join -creates a string from the created list
    print(rev_string)
    #REMOVE SPECIAL CHARACTERS- IM THINKING OF A LOOPING THROUGH, BUT NOT SURE HOW TO IMPLEMENT IT
    string=string.replace(' ', '')
    rev_string=rev_string.replace(' ', '')
    string=string.replace(',', '')
    rev_string=rev_string.replace(',', '')
    string=string.replace('.', '')
    rev_string=rev_string.replace('.', '')
    #THIS IS THE LOGIC: IT CHECKS BOTH STRINGS, if they are equal, it is a palindrome; 
    if string.lower()==rev_string.lower():
        return True, print('It is a Palindrome')
    else:
        return False, print('It isnt a palindrome')

#call the function; key in the parameters- 
palindrome= palindrome("No, Mel Gibson Is A Casinos Big Lemon")
#maybe we can try having a user key in the parameter? lets try
#palindrome=palindrome(input('kindly enter your word/phrase  '))-wrong
#print('Kindly enter your word or phrase')
#user_palindrome=input('')
#palindrome=palindrome(user_palindrome)
#it wont work this way either

如果您可以让用户定义参数(字符串),如果您知道如何定义,请与他人分享。

要检查单词或短语是否为回文,必须检查原始句子是否与原始句子相同

word = "Eva can I see bees in a cave"

word_lower = word.lower().replace(" ", "")

if word_lower == word_lower[::-1]:
    print("It's a palindrome")
else:
    print("This is not a palindrome")
在python-3中

name = 'madam'
print(name.find(name[::-1]) == 0)

返回在循环内?这段代码有很多错误,不仅仅是它不起作用。@Bobby嗯。。。如果你要提出批评,至少要准确。OP确实给出了一个他们尝试的例子。问题更多的是“为什么我的代码不起作用”和“帮助我完成家庭作业”。如果这个词不是回文,你会怎么做?你永远不会返回
False
。也许,因为这是一个课堂/家庭作业,只是在评论中提供一个可能的提示,而不是为他们做家庭作业?考虑到他们问题的基本性质和他们编写的代码,我甚至不认为OP已经学会了你使用的语法。而且,你发布的内容与他们编写的完全不同。您基本上发布了自己的解决方案,而忽略了他们的解决方案。另外,为什么
如果perfectlyfineboolyoucouldjustreturn:return True否则:return False