-2

This is my first program ever in Python, in fact in any language.

I get a TypeError and I am unable to solve it!

This is my code: (the error is below)

#! usr/bin/python
import time
import random

print("HI WELCOME TO HANGMAN !")
print("\n")
time.sleep(1.5)
name = input("Enter your name :")
print("\n")
print("HEY " + name + " HOPE YOU HAVE A GOOD TIME PLAYING HANGMAN TODAY")
print("\n")
time.sleep(1.5)
print("YOUR GAME IS ABOUT TO BEGIN IN A FEW SECONDS")
time.sleep(1)


class Hangman():
    def __init__(self, word, guesses, count, limit) :
        self.count = count
        self.display= "_" * self.length 
        self.word = word 
        self.length = len(word)
        self.guessed = []
        self.guesses = guesses
        self.Continue = ""
        self.limit = limit

    def get_word(self):
        words = ["book" ,"paper", "computer", "movie", "mouse", "laptop", "national"]
        self.word = random.choice(words)
    
    def play_loop(self):
        self.Continue = input("Do you wish to play another round? y = yes, n = no")
        if self.Continue == 'y':
            Hangman()
        elif self.Continue == "n":
            print("thanks for playing! hope you had a good time :)")
            exit()

    def play(self,guesses):
        self.count = 5
        print("your word is " + self.word + "enter your guesses ")
        self.guesses = guesses.strip()
        if len(guesses.strip()) == 0:
            print("Invalid input, please enter a letter \n")
        elif len(guesses.strip()) >=2:
            print("Invalid input, please enter one letter at a time \n")

        elif self.guesses in self.word :
            self.guessed.extend(self.guesses)
            self.index = self.word.find(self.guesses)
            self.word = self.word[self.index] + '_' + self.word[self.index + 1]
            self.display = self.display[:self.index] + self.guesses + self.display[:self.index + 1]
            print(self.display + "\n")

        else:
            self.count +=1

        if self.count == 1:
            time.sleep(1)
            print("   _____ \n"
                   "  |      \n"
                   "  |      \n"
                   "  |      \n"
                   "  |      \n"
                   "  |      \n"
                   "  |      \n"
                   "__|__\n")
            print(" wrong guess,you have" + str(self.limit - self.count) + "guesses remaining")
        
        elif self.count == 2:
            time.sleep(1)
            print("   _____ \n"
                "  |     | \n"
                "  |     |\n"
                "  |      \n"
                "  |      \n"
                "  |      \n"
                "  |      \n"
                "__|__\n")
            print("wrong guess, you have " + str(self.limit - self.count) + "guesses remaining")

        elif self.count == 3:
            time.sleep(1)
            print("   _____ \n"
                "  |     | \n"
                "  |     |\n"
                "  |     | \n"
                "  |      \n"
                "  |      \n"
                "  |      \n"
                "__|__\n")
            print("wrong guess, you have") + str(self.limit - self.count) + "guesses remaining"

        elif self.count == 4:
            time.sleep(1)
            print("   _____ \n"
                "  |     | \n"
                "  |     |\n"
                "  |     | \n"
                "  |     O \n"
                "  |      \n"
                "  |      \n"
                "__|__\n")

        elif self.count == 5:
            time.sleep(1)
            print("   _____ \n"
                "  |     | \n"
                "  |     |\n"
                "  |     | \n"
                "  |     O \n"
                "  |    /|\ \n"
                "  |    / \ \n"
                "__|__\n")
            print("wrong guesses, you are hanged!")
            print("the word was" , self.word)
            Hangman.play_loop()

        if self.display:
            print("congrats you won !")
            Hangman.play_loop()
        elif self.count != self.limit:
            Hangman.play_loop()

Hangman_game = Hangman()
Hangman_game()   
        

This is the error I get:

Traceback (most recent call last):
  File "/Users/angad/Desktop/hangman(no global).py", line 126, in <module>
    Hangman_game = Hangman()
TypeError: __init__() missing 4 required positional arguments: 'word', 'guesses', 'count', and 'limit'
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 3
    Isn't the error message clear? The `__init__` method of your `Hangman` class requires 4 arguments (word, guesses, count, limit) but you are trying to instantiate it using `Hangman()` with no arguments. – Selcuk Oct 26 '20 at 06:17
  • 1
    why does play() overwrite `self.count` ? – Patrick Artner Oct 26 '20 at 06:29
  • 1
    what should `Hangman_game()` accomplish? Why do you have a method for using a random word from a given wordlist and also provide a word to be guessed on initialization? Why do you print the word that needs to be guessed as plain text in your `play()` function? – Patrick Artner Oct 26 '20 at 06:33
  • 1
    Tangentially, `usr/bin/python` tries to find `python` inside `bin` inside `usr` *in the current directory.* You need to add a slash to make it absolute `/usr/bin/python`; see perhaps also https://stackoverflow.com/questions/31435921/difference-between-and – tripleee Oct 26 '20 at 06:44
  • patrick, self.count is actually the number of times a person guesses a wrong alphabet and thanks to that I found another mistake that I can correct.. thanks for the input on the self.word being in the __init__ and as a separate function, I have changed the code accordingly .... talking about the point why I print the word that needs to be guessed as plain text in play() function, are you talking about: print(self.display + "n")? –  Oct 26 '20 at 13:07
  • @ANGADSINGHBEDI Use ‘@’ to ping a previous commentator. Regarding the printing of the word that needs to be guessed, I guess they are talking about: `print("your word is " + self.word + "enter your guesses ")` – Brian Drake Oct 26 '20 at 13:35
  • @ANGADSINGHBEDI `__init__()` sets `self.count`, but this value is never used, becuase the very first thing that `play()` does is to overwrite it. Why? – Brian Drake Oct 26 '20 at 13:39
  • @BrianDrake thanks for that :)... yeah and about the self.count, I changed that, I removed a few initialisations and hope that the code is better.... i also changed the self.word to self.display so that should be fine now ....when I see another problem I am unable to solve, I will put it here:) –  Oct 26 '20 at 13:47

1 Answers1

1

Look at Hangman class.

def __init__(self, word, guesses, count, limit) :

you need 4 arguments to initialize that class. Yet you did not put any when initializing.

Hangman_game = Hangman()
Alvi15
  • 335
  • 2
  • 10