1
class Dog:
   tricks = []
   def __init__(self,name):
     self.name=name
   def add_tricks(self, trick ):
     self.tricks.extend(trick)

I keep getting the error that add_tricks only accepts 1 positional arguments but 3 were given. I tried passing **kwargs to the function but that didn't work either. How do I pass multiple strings to the function?

0

1 Answer 1

2

For (*args) parameters as ("Hello","Byy")

class Dog:
   tricks = []
   def __init__(self,name):
     self.name=name
   def add_tricks(self, *trick):
     self.tricks.extend(trick)

For (**kwargs) parameters as (a="Hello",b="Byy")

class Dog:
   tricks = []
   def __init__(self,name):
     self.name=name
   def add_tricks(self, **trick):
     self.tricks.extend(trick.values())

Or if you want to give string in both types like ("Hello",b="byy")

class Dog:
   tricks = []
   def __init__(self,name):
     self.name=name
   def add_tricks(self, *trick1,**trick2):
     self.tricks.extend(trick)
     self.tricks.extend(trick2)
Sign up to request clarification or add additional context in comments.

2 Comments

So it works when I use the *args method but produces errors using **kwargs like in your second code. "add_tricks() takes 1 positional argument but 3 were given"
a = Dog("bruno") a.add_tricks("fetch", "roll") This is what I am trying to add

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.