0

I'm new to python and I'm trying to do the "Band name generator" kata on codewars but I'm having trouble with the join function and I do not know why. It says "can only join an iterable" but I am trying to join a list which is an iterable so I do not understand what is the issue here and how to solve this. Thank you so much in advance!

import re
regex = r'^[a-z]$|^([a-z]).*\1$'

def split(name):
    return [char for char in name]


def band_name_generator(name):
    if (re.search(regex, name)):
        x = split(name).append(split(name)[1:])
        return "".join(x)
    else:
        return "The " + name.capitalize()

print(band_name_generator("alaska"))
philipxy
  • 14,867
  • 6
  • 39
  • 83
  • You can really simplify the logic - see the post below. – Daniel Hao Jun 13 '22 at 23:50
  • Welcome to Stack Overflow. In your own words, what do you expect `split(name).append(split(name)[1:])` to mean? What result do you expect `x` to have? Did you *check* this? Did you try to read the documentation for the `.append` method? Also, when asking a question, please show a [complete](https://meta.stackoverflow.com/questions/359146) error message. Generally, they explain exactly what is wrong and where in the code the problem occurred - when properly read and understood. – Karl Knechtel Jun 14 '22 at 00:10

1 Answers1

1

First of all, the built-in function list() does the same as your split function, so I would suggest using that if you can.

You can also splice strings as if they were lists and then join them together so the function band_name_generator() could be rewritten as follows

def band_name_generator(name):
    if (re.search(regex, name)):
        x = name + name[1:]
        return x
   
    return "The " + name.capitalize()

The final else statement is not necessary as when a function returns it stops.

I would also suggest to use regex.compile as recommended by the official docs.

import re
p = re.compile(r'^[a-z]$|^([a-z]).*\1$')
p.search(name)

In any case, the problem in your code is that append() does not return a new list but modifies the original one, so your value x is empty (NoneType)

Ftagliacarne
  • 675
  • 8
  • 16