[英]IndexError string index out of range
s="(8+(2+4))"
def checker(n):
if len(n) == 0:
return True
if n[0].isdigit==True:
if n[1].isdigit==True:
return False
else:
checker(n[1:])
else:
checker(n[1:])
This is what I have so far. 这就是我到目前为止所拥有的。 Simple code, trying to see if a string meets the following conditions.
简单的代码,试图查看字符串是否满足以下条件。 However when i perform checker(s) i get:
但是当我执行检查时,我得到:
True
IndexError: string index out of range
Any help? 有帮助吗? Thanks in advance Edit: The function's purpose is to produce true if the string contains only single digit numbers, and false if 2 or more-figured digits exist in the string.
提前谢谢编辑:如果字符串只包含单个数字,则函数的目的是产生true;如果字符串中存在2个或更多数字的数字,则产生false。
When the length of n
is 0, the n[0]
part is going to raise an error because the string in empty. 当
n
的长度为0时, n[0]
部分将引发错误,因为该字符串为空。 You should add a return
statement there instead of print. 你应该在那里添加一个
return
语句而不是print。
def checker(n):
if len(n) < 2:
return True
if n[0] in x:
Note that the conditions must be len(n) < 2
otherwise you'll get an error on n[1]
when the length of string is 1. 请注意,条件必须为
len(n) < 2
否则当字符串的长度为n[1]
时,您将在n[1]
上收到错误。
Secondly you're trying to match characters to a list which contains integers, so the in checks are always going to be False
. 其次,您正在尝试将字符匹配到包含整数的列表,因此in检查始终为
False
。 Either convert the list items to string or better use str.isdigit
. 将列表项转换为字符串或更好地使用
str.isdigit
。
>>> '1'.isdigit()
True
>>> ')'.isdigit()
False
>>> '12'.isdigit()
True
Update: 更新:
You can use regex
and all
for this: 你可以使用
regex
和all
这些:
>>> import re
def check(strs):
nums = re.findall(r'\d+',strs)
return all(len(c) == 1 for c in nums)
...
>>> s="(8+(2+4))"
>>> check(s)
True
>>> check("(8+(2+42))")
False
Working version of your code: 代码的工作版本:
s="(8+(2+4))"
def checker(n):
if not n: #better than len(n) == 0, empty string returns False in python
return True
if n[0].isdigit(): #str.digit is a method and it already returns a boolean value
if n[1].isdigit():
return False
else:
return checker(n[1:]) # use return statement for recursive calls
# otherwise the recursive calls may return None
else:
return checker(n[1:])
print checker("(8+(2+4))")
print checker("(8+(2+42))")
output: 输出:
True
False
You should do return True
after the first if statement, not print True
. 你应该在第一个if语句后
return True
,而不是print True
。 The function continues to run after that statement and hits an error when the input is size 0. 该函数在该语句之后继续运行,并在输入大小为0时遇到错误。
I can't reproduce your error. 我无法重现你的错误。
I had to fix a few things: 我不得不解决一些问题:
.isdigit()
is a function; .isdigit()
是一个函数; calling .isdigit==True
is going to compare a function object with True, which is never going to be true. .isdigit==True
将比较一个函数对象与True,这永远不会是真的。 I changed .isdigit==True
to .isdigit()
.isdigit==True
为.isdigit()
Aside from that, a few print statements show that this is working as expected 除此之外,一些印刷声明表明这是按预期工作的
s="(8+(2+4))"
t="(8+(20+4))"
def checker(n):
print "checking %s" % n
if len(n) == 0:
print "Returning true"
return True
if n[0].isdigit():
if n[1].isdigit():
print "returning false"
return False
else:
return checker(n[1:])
else:
return checker(n[1:])
print checker(s)
print checker(t)
Output: 输出:
checking (8+(2+4))
checking 8+(2+4))
checking +(2+4))
checking (2+4))
checking 2+4))
checking +4))
checking 4))
checking ))
checking )
checking
Returning true
True
checking (8+(20+4))
checking 8+(20+4))
checking +(20+4))
checking (20+4))
checking 20+4))
returning false
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.