1 回答
TA贡献1802条经验 获得超5个赞
问题在于您有一个游戏状态,您正在将其作为全局变量进行跟踪,guess_count并且count_control
这是为什么 python 和其他语言提供类和对象的示例:
class Game:
def __init__(self):
self.guess_count = []
self.count_control = 1
@staticmethod
def check_guess(letter, guess):
if guess.isalpha() == False:
print("Invalid!")
return False
elif guess.lower() < letter:
print("Low")
return False
elif guess.lower() > letter:
print("High")
return False
elif guess.lower() == letter:
print("Correct!")
return True
else:
print("anything")
def letter_guess(self, guess):
self.check_guess('a', guess)
while len(self.guess_count) <= 3:
if self.check_guess('a', guess) == True:
return True
elif self.check_guess('a', guess) == False:
self.guess_count.append(self.count_control)
guess = input("Try again \n")
game = Game()
game.letter_guess(input("test: "))
game = Game()
game.letter_guess(input("test: "))
添加回答
举报