Python类没有';行不通
当我运行这段代码时,它确实会进入Python类没有';行不通,python,class,Python,Class,当我运行这段代码时,它确实会进入\u init\u函数,我不知道为什么。我在结尾用hangman=Main()调用它,但它不起作用 class Main: wordToGuess = 'world cup' displayWord = '***** ***' num_wrong = 0; def _init_(self): print('lets play!') while True: lette
\u init\u
函数,我不知道为什么。我在结尾用hangman=Main()
调用它,但它不起作用
class Main:
wordToGuess = 'world cup'
displayWord = '***** ***'
num_wrong = 0;
def _init_(self):
print('lets play!')
while True:
letter = input('Enter guess: ')
isDansWord = self.isInWord(letter)
if isDansWord:
self.updateDisplayWord(letter)
print(self.displayWord)
if self.checkForWin():
self.win()
break
else:
print('wrong')
self.num_wrong += 1
if self.checkForLoss():
self.loss()
break
def isInWord(self,letter):
inWord = False
for x in range(0, len(self.displayWord)):
if self.wordToGuess[x:x + 1] == letter:
inWord = True
break
return inWord
def checkForWin(self):
if self.wordToGuess == self.displayWord:
return True
else:
return False
def checkForLoss(self):
if self.num_wrong > 5:
return True
else:
return False
def win(self):
print('you win!')
def loss(self):
print('you lose!')
print('Hangman\n')
hangman = Main()
您需要4个下划线:
它的
\uuu init\uuuuu
,而不是\uinit\uuu尝试不\uinit\uuu
,而是\uu init\uuuuu
(两边各两个“)\uu init\uuu
方法是Python中的一种方法。特殊方法以双下划线开始和结束,因此您的方法应为:
def __init__(self):