所以,我用这段代码来检查一个数字是否是回文,它工作得很好,但是我有一个关于在while循环中使用某个操作符的问题。在原始变量和整数//之间使用的操作符10。它对原始的p值做了什么,是分割的还是分割的?下面是正在使用的代码
def test_palindrome(p):
o=p#store the original value of p in some variable
reversed_number=0#declare reversed_number and init to zero
while(p>0):
rem=p%10#Get the remainder of argument and ten
reversed_number=reversed_number*10+rem
p=p//10#This is the operator whose function is in question, am not sure if its dividing
if(reversed_number==o):#do comparison with original input and return
print(f"{o} is a palindrome")
else:
print(f"{o} is not a palindrome")
test_palindrome(number)发布于 2021-03-25 15:59:15
//指的是楼层划分。地板除法将始终给出结果的整数地板。
您的程序首先检查是否为p>0。让我们说p = 1001。
1001 // 10 = 100而1001/10 = 100.1
如果您使用的是p/10而不是p//10。p绝不会比0小。小数:0.1 (示例)总是存在的。因此,条件p>0将永远是真实的,破坏您的程序。
正如注释中所提到的,this帖子可能有用。
https://stackoverflow.com/questions/66803104
复制相似问题