首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在python中检查回文号时运算符//的含义是什么?

在python中检查回文号时运算符//的含义是什么?
EN

Stack Overflow用户
提问于 2021-03-25 15:52:15
回答 1查看 106关注 0票数 0

所以,我用这段代码来检查一个数字是否是回文,它工作得很好,但是我有一个关于在while循环中使用某个操作符的问题。在原始变量和整数//之间使用的操作符10。它对原始的p值做了什么,是分割的还是分割的?下面是正在使用的代码

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-25 15:59:15

//指的是楼层划分。地板除法将始终给出结果的整数地板。

您的程序首先检查是否为p>0。让我们说p = 1001

1001 // 10 = 1001001/10 = 100.1

如果您使用的是p/10而不是p//10p绝不会比0小。小数:0.1 (示例)总是存在的。因此,条件p>0将永远是真实的,破坏您的程序。

正如注释中所提到的,this帖子可能有用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66803104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档