簡體   English   中英

類型錯誤:__init__() 缺少 4 個必需的位置參數:

[英]TypeError: __init__() missing 4 required positional arguments:

這是我用 Python 編寫的第一個程序,實際上是用任何語言編寫的。

我收到一個 TypeError 並且我無法解決它!

這是我的代碼:(錯誤如下)

#! 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()   
        

這是我得到的錯誤:

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'

看看劊子手課。

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

您需要 4 個參數來初始化該類。 然而你在初始化時沒有放任何東西。

Hangman_game = Hangman()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM