检查一个单词是否是回文
Checking if a word is a palindrome
我正在编写一个函数来检查一个单词是否是回文
def palidrome(b):
word = ''.join(reversed(b))
if b == word:
return True
return False
def main():
so = input("Please enter a matching word")
come = palidrome(so)
print(come)
main()
无论我输入什么,例如 'mom,' 'dad' 或 'racecar,' 它总是输出 False
但它应该是 True
.
根据 this demo, your code is running fine - however, I noticed your input statement doesn't have a space after it. Are you typing a space before you put your word in? If so, consider the strip()
函数,这将删除前导和尾随 spaces - 或者只是在您的输入提示中添加一个 space!
def checkPalindrome(word):
wordCopy = word[::-1]
if word == wordCopy:
return True
else:
return False
def main():
s = 'oro'
print(checkPalindrome(s))
main()
我正在编写一个函数来检查一个单词是否是回文
def palidrome(b):
word = ''.join(reversed(b))
if b == word:
return True
return False
def main():
so = input("Please enter a matching word")
come = palidrome(so)
print(come)
main()
无论我输入什么,例如 'mom,' 'dad' 或 'racecar,' 它总是输出 False
但它应该是 True
.
根据 this demo, your code is running fine - however, I noticed your input statement doesn't have a space after it. Are you typing a space before you put your word in? If so, consider the strip()
函数,这将删除前导和尾随 spaces - 或者只是在您的输入提示中添加一个 space!
def checkPalindrome(word):
wordCopy = word[::-1]
if word == wordCopy:
return True
else:
return False
def main():
s = 'oro'
print(checkPalindrome(s))
main()