python - Python 代码中没有
问题描述
我是 python 新手,为了测试它,我创建了一个简单的回文程序。我面临一个问题,每当我将参数传递给我的函数时,我都会返回一个带有所需答案的“无”。谁能告诉他们这背后的原因。请看一下我的代码
try:
def reverse_string1(s):
return s[::-1]
def calculate_palindrome(c):
c = c.lower()
b = reverse_string1(c)
if c == b:
print("String {0} is palindrome".format(c))
else:
print("String {0} is not palindrome".format(c))
print(calculate_palindrome('Anna'))
print(calculate_palindrome('caca'))
print(calculate_palindrome('alula'))
print(calculate_palindrome('test'))
except Exception as error:
print(error)
我得到以下回报
String anna is palindrome
None
String mama is not palindrome
None
String alula is palindrome
None
String test is not palindrome
None
提前致谢!
解决方案
您必须使用“return”从函数中返回一个值。否则默认返回无。您没有从 calculate_palindrome 返回任何内容。