2

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() 
2
  • 1
    What do you think happens if len(s) % 2 == 0? Commented Feb 4, 2021 at 2:14
  • Omg, I'm dumb, sorry I'm new to this! Commented Feb 4, 2021 at 2:34

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.