整数alind植物
x = int(input())
if x<0 and (x%10 == 0 and x !=0):
print("false")
rev = 0
while x>0:
rev = rev*10 + x%10
x = x // 10
if rev == x:
print("true")
else:
print("false")
由于相反的数字与输入的数字相同,因此条件rev == x
应返回true,但给出了相反的情况。我在做什么错?
编辑1:不允许将转换为字符串
编辑2:
x = int(input())
if x<0 and (x%10 == 0 and x !=0):
print("false")
rev = 0
while x>0:
rev = rev*10 + x%10
x = x // 10
if rev == x:
print("true")
else:
print("false")
Since the reversed number is the same as entered one, the condition rev == x
should return true but it's giving the opposite. What am I doing wrong?
Edit 1: converting to string is not allowed
Edit 2: ???? I see now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
建议:
您正在阅读
x
byinput()
,因此,检查它是否会逆转它是一个更好的想法:更好地理解
[:: - 1 ]
我建议您阅读this 。
回答:
的评论中的某人退出,这是您真正的问题。
更清楚地说,我建议您阅读代码的这一部分,并想知道
the
loop 循环结束时x
的值是什么。Suggestion:
You are reading
x
byinput()
, so a better idea to check if it's palindrome would be reversing it:To better understand
[::-1]
I would suggest you to readthis
.Answer:
Somebody in the comments said this, which is your real problem.
To be clearer I would suggest you to read this part of your code, and to wonder what will be the value of
x
at the end of thewhile
loop.无论输入X等于while循环之后,您都在段循环内更新“ x”值。
在输入()之后,将X值复制到某个温度变量,并在IF条件下进行比较。
You are updating the 'x' value inside the while loop, irrespective of the input x equals 0 after the while loop.
Copy x value to some temp variable after the input() and compare that in the if condition.