Reputation: 57
Here's the question I'm working on!
Split Strings
Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').
def solution(s):
answer = []
a = 0
if (len(s) % 2) != 0:
a = s + "_"
for i in range(len(a) // 2):
answer.append(a[2 * i:2 * i + 2])
return answer
Traceback (most recent call last):
File "main.py", line 13, in <module>
test.assert_equals(solution(inp), exp)
File "/home/codewarrior/solution.py", line 6, in solution
for i in range(len(a) // 2):
TypeError: object of type 'int' has no len()
Upvotes: 2
Views: 211
Reputation: 1481
The issue is you are initializing a as an integer a=0
If s is even, the line a = s + "_"
is not executed. So a
remains an integer in that case.
Which is why (len(a) // 2)
gives an error. len
does not work with integers, only with strings.
To fix the issue, initialize a=s
Upvotes: 3