回文检查代码在def内部无法工作。

huangapple go评论46阅读模式
英文:

Palindrome checking code does not work inside def

问题

为什么这个回文检查函数不起作用?

def palindrome(a):
b = (str(a)[::-1])
if a == b:
print("回文")
else:
print("不是回文")

palindrome(121)


输出

```none
不是回文
英文:

Can anyone check why this palindrome checking function is not working?

def palindrome(a):
    b = (str(a)[::-1])
    if a == b:
        print("palindrome")
    else:
        print("not palindrome")

palindrome(121)

output

not palindrome

答案1

得分: 0

a不是一个字符串,所以在if语句之前添加以下代码:
a = str(a)

英文:

a is not a string so just add this code to it before the if statement.
a = str(a)

huangapple
  • 本文由 发表于 2023年7月11日 06:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定