Adi_Sharma
Adi_Sharma

Reputation: 3

How to solve method() takes 1 positional argument but 2 were given, when i pass a variable into another method

I want to pass this chance variable declared in main_game in Hangman_figure

def hangman_figure(chance):
    print("hello world")

    if chance==8:

        print("Select another letter")
        print(chance)

    elif chance== 7:
        print("O")
    elif chance==6:
        print("O")
        print('|')

this is the method where my variable is declared

def main_game(self):

    spaces=[]
    chance=9
    for space in range(0,self.word_len):
        spaces.append('_ ')
        print(spaces[space],end=" ")


    for x in range(1,10):
        choose =input('Kindly choose a letter that you think this word contains :')
        if choose in self.word_store:
            position=self.word_store.index(choose)
            spaces[position]= choose
            for y in range(0,self.word_len):
                print(spaces[y],end=" ")


            print("Great!! This letter is present in the word, Keep Going")

        else:
            chance=chance-1 
            #I have declared this chance variable which I need to use

            print("Sorry this letter does not exist in the word")

            self.hangman_figure(chance)   

How can I pass this chance variable in my hangman_figure method

Upvotes: 0

Views: 67

Answers (2)

awarrier99
awarrier99

Reputation: 3855

It's hard to tell from your code formatting and limited snippets, but from your usage of self.hangman_figure it seems like hangman_figure is a class method, so you need to add an argument for self:

def hangman_figure(self, chance):

You're receiving this error because Python implicitly passes an instance of the class itself in the place of the self argument, so with your definition of just def hangman_figure(chance), it interprets the chance argument to act as the self argument (because the self argument doesn't actually have to be named self), so when you pass in another argument with self.hangman_figure(chance), it's raising the error because you are passing two arguments (including the implicit self) instead of the one argument you had included in your original function definition

Upvotes: 1

Banana
Banana

Reputation: 2533

I guess you're using a class. That given your method hangman_figure needs to have self as an argument. Corrected method:

def hangman_figure(self, chance):
   print("hello world")

Otherwise self.hangman_figure(chance) in main.py will cause the error because you call the method on a class instance which counts as giving an argument and you also give chance as an argument.

Upvotes: 1

Related Questions