超出范围错误?
Out of range error?
我正在构建一个函数,它接受表示多行的字符串(文本)并计算有多少行以数字 (0-9) 开头,然后 returns 一个整数作为计数结果。
我使用 split 获取每一行,然后获取检查第一个索引的函数。我收到了一个超出范围的错误,因为一些测试用例的第一个索引是空格。我该如何解决这个问题?
这是确切的错误:
错误:test_count_digit_leading_lines_14(main.AllTests)
回溯(最后一次调用):
文件 "testerl7.py",第 88 行,在 test_count_digit_leading_lines_14 中
def test_count_digit_leading_lines_14 (self): self.assertEqual (count_digit_leading_lines("\t4G\n4G\n4\tG\n\n4000\n55\nsix\n7 天"),5)
文件“/Users/kgreenwo/Desktop/student.py”,第 8 行,在 count_digit_leading_lines 中
check=newlines[i][0] #检查每行的第一个字符
IndexError: 字符串索引超出范围
以下是示例输出:
count_digit_leading_lines ("AAA\n1st") → 1 # 2nd line starts w/ digit 1
count_digit_leading_lines("\t4G\nHz\n") → 0 # 1st line starts w/ tab
count_digit_leading_lines ("0\n0 3\nn4\n") → 2
这是我当前的代码:
def count_digit_leading_lines(text):
count=0
if text == " ":
return 0
else:
newlines=text.rstrip('\n').split('\n') #split each line
for i in range(len(newlines)):
if newlines[i] != None:
check=newlines[i][0] #check first character in each line
if check.isdigit(): #if 1st is digit 0-9
count+=1
return count #return how many times
换行数组中可能有长度为0的字符串。您应该先删除此类元素:
# add this line newlines = [ a for a in newlines if len(a) > 0 ]
# as shown below:
def count_digit_leading_lines(text):
count=0
if text == " ":
return 0
else:
newlines = text.rstrip('\n').split('\n') #split each line
newlines = [ a for a in newlines if len(a) > 0 ]
for i in range(len(newlines)):
check=newlines[i][0] #check first character in each line
if check.isdigit(): #if 1st is digit 0-9
count+=1
我正在构建一个函数,它接受表示多行的字符串(文本)并计算有多少行以数字 (0-9) 开头,然后 returns 一个整数作为计数结果。
我使用 split 获取每一行,然后获取检查第一个索引的函数。我收到了一个超出范围的错误,因为一些测试用例的第一个索引是空格。我该如何解决这个问题?
这是确切的错误:
错误:test_count_digit_leading_lines_14(main.AllTests)
回溯(最后一次调用): 文件 "testerl7.py",第 88 行,在 test_count_digit_leading_lines_14 中 def test_count_digit_leading_lines_14 (self): self.assertEqual (count_digit_leading_lines("\t4G\n4G\n4\tG\n\n4000\n55\nsix\n7 天"),5) 文件“/Users/kgreenwo/Desktop/student.py”,第 8 行,在 count_digit_leading_lines 中 check=newlines[i][0] #检查每行的第一个字符 IndexError: 字符串索引超出范围
以下是示例输出:
count_digit_leading_lines ("AAA\n1st") → 1 # 2nd line starts w/ digit 1
count_digit_leading_lines("\t4G\nHz\n") → 0 # 1st line starts w/ tab
count_digit_leading_lines ("0\n0 3\nn4\n") → 2
这是我当前的代码:
def count_digit_leading_lines(text):
count=0
if text == " ":
return 0
else:
newlines=text.rstrip('\n').split('\n') #split each line
for i in range(len(newlines)):
if newlines[i] != None:
check=newlines[i][0] #check first character in each line
if check.isdigit(): #if 1st is digit 0-9
count+=1
return count #return how many times
换行数组中可能有长度为0的字符串。您应该先删除此类元素:
# add this line newlines = [ a for a in newlines if len(a) > 0 ]
# as shown below:
def count_digit_leading_lines(text):
count=0
if text == " ":
return 0
else:
newlines = text.rstrip('\n').split('\n') #split each line
newlines = [ a for a in newlines if len(a) > 0 ]
for i in range(len(newlines)):
check=newlines[i][0] #check first character in each line
if check.isdigit(): #if 1st is digit 0-9
count+=1