0

I'm trying to split a string at its commas in Python at the moment and I'm getting the error main() missing 1 required positional argument: 'self' This is what I have as of right now.

def main(self):
    last_letters('Ani,Trevor,Karen,Jasmine,Ryan')

def last_letters(self):
    last_letters1 = last_letters.split(",")
if __name__ == '__main__':
    main()

EDIT: After I remove self from the def, I am still left with this error--

`Traceback (most recent call last):
  File "/home/main.py", line 13, in <module>
    main()
  File "/home/main.py", line 2, in main
    last_letters('Ani,Trevor,Karen,Jasmine,Ryan')
TypeError: last_letters() takes 0 positional arguments but 1 was given

`

heth123
  • 31
  • 4

3 Answers3

1

If you declare that a function takes a positional argument, then you must supply that argument when calling the function.

As it is, your main() function does not require any argument so just change it to:

def main():
    last_letters('Ani,Trevor,Karen,Jasmine,Ryan')

Also, the argument name self is by convention used as a reference to an object instance within the object itself. You find it as the first argument to a method (a function that is bound to an instance of an object), whereas you are using standalone function.

The argument to last_letters() is the string 'Ani,Trevor,Karen,Jasmine,Ryan' when that function is called in main(). last_letters() does require the argument, but for the aforementioned reasons it should not be named self. You could just call it s or something descriptive of its value.

Finally the line last_letters1 = last_letters.split(",") won't work. You need to call split() on a string instance, like this:

def last_letters(text):
    names = text.split(",")
    # do something with names...
    # return a list containing the last letter of each name
    return [name[-1] for name in names]
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Ohhhh I see. I added that after getting this error (which reoccurs when I don't have `self` there. It reads " `Traceback (most recent call last): File "/home/main.py", line 13, in main() File "/home/main.py", line 2, in main last_letters('Ani,Trevor,Karen,Jasmine,Ryan') TypeError: last_letters() takes 0 positional arguments but 1 was given` " – heth123 Jan 11 '21 at 03:33
0

I suggest you write code first without functions as this is a small program and once it's running, slowly convert into functions and this helps in understanding it without functions.

if __name__ == '__main__':
    last_letters="Ani,Trevor,Karen,Jasmine,Ryan"
    last_letters1 = last_letters.split(",")
    print(last_letters1)

The mistake you did is you're trying to pass str/word but you didn't specify that in the function (just give a name so that value you passed will be stored in it)

def main():
    last_letters('Ani,Trevor,Karen,Jasmine,Ryan')

def last_letters(word):
    last_letters1 = word.split(",")
    print(last_letters1)

if __name__ == '__main__':
    main()

Exactly your code updated

def main():
    last_letters('Ani,Trevor,Karen,Jasmine,Ryan')

def last_letters(last_letters):
    last_letters1 = last_letters.split(",")
    print(last_letters1) # to print data
if __name__ == '__main__':
    main()
CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
0

There's a number of issues:

  • You call main() in and appropriate way, but as you can tell from the call main(), it doesn't need a parameter, so instead of declaring it with self as a parameter, it could just be:
def main():
    last_letters('Ani,Trevor,Karen,Jasmine,Ryan')
  • Similarly, last_letters doesn't need self either. Typically, you only see self as a parameter in methods on a class, but these are just functions by themselves (and even for a class, the parameter doesn't need to be called self, it's just a convention). However, you do need to pass a text to last_letters, so it makes sense to define a parameter like text:
def last_letters(text):
    return text.split(",")
  • Also note that I changed the code of the function to return the result of the split, this ensures that the result is available wherever the function was called, if you do something with it, like capture it in a variable, or print it. Let's print it from main():
def main():
    print(last_letters('Ani,Trevor,Karen,Jasmine,Ryan'))
  • Since your function is called last_letters, you probably just want the last letters:
def last_letters(text):
    return [word[-1] for word in text.split(",")]

So, the result is then:

def main():
    print(last_letters('Ani,Trevor,Karen,Jasmine,Ryan'))


def last_letters(text):
    return [word[-1] for word in text.split(",")]


if __name__ == '__main__':
    main()

And the output:

['i', 'r', 'n', 'e', 'n']
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Uhhh you are a literal magician. Where did you learn all of this?? Do you have any favorite resources you refer to at all? TEACH ME YOUR WAYS. Additionally, instead of separating the last letter by a comma, I'm trying to put them on separate lines. Do you have any idea how I might go about doing this? – heth123 Jan 11 '21 at 03:45
  • One step at a time, but there's plenty of excellent Python courses and resources out there. The official Python documentation is actually quite well-written and useful, but you need to know the basics to make sense of it. Learning the basics is a matter of working your way through a course of your liking. As for printing them on individual likes, you can just assign that list to a variable `my_list = ...` and then loop over the list `for letter in my_list: print(letter)` – Grismar Jan 11 '21 at 04:00
  • Would that end up looking like `my_list = last_letters for letter in my_list: print(letter)` ? – heth123 Jan 11 '21 at 04:10
  • Give it a try yourself - you'll get it to work; learning how to code is 99% trying stuff and learning by making mistakes. – Grismar Jan 11 '21 at 04:27
  • Yeah, I pretty much ended up figuring it out haha. Thank you for your help!! – heth123 Jan 11 '21 at 04:28
  • I know have a separate problem with only 10 minutes left for me to solve it. At the end of my output, "None" is appearing. And I'm trying to figure out how to get rid of it. I thought using filter() would be okay, but I can't seem to get it to work. – heth123 Jan 11 '21 at 04:46