[英]Check if a number has same digit in Python
How to check if a number has same digit in Python如何检查Python中的数字是否相同
For example:例如:
check(111) --> True
check(22) --> True
check(111) --> True
check(45) --> False
Are you expecting an answer like this?你期待这样的答案吗? It will return True if all the digits are same else False.如果所有数字都相同,它将返回 True,否则返回 False。
What I am doing in this answer is, I am taking the first digit then I am multiplying the string form of the digit times the length of the string and using an If statement to check whether they match or not.我在这个答案中所做的是,我取第一个数字,然后将数字的字符串形式乘以字符串的长度,并使用 If 语句检查它们是否匹配。
def check(n):
if str(n) == str(n)[0] * len(str(n)):
return True
else:
return False
print(check(45))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.