我开始学习 Python,分配给我的任务是为一个猜谜游戏编写代码,在这个游戏中我必须控制玩家的总尝试次数。我已经描述了这些功能,它们正在工作(我相信......哈哈)但是当输入错误的猜测时我无法“重置”游戏......我写了这个:
guess_count = []
count_control = 1
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(guess):
check_guess ('a',guess)
while len(guess_count) <= 3:
if check_guess == True:
return True
elif check_guess == False:
guess_count.append(count_control)
guess = input("Try again \n")
letter_guess(input("test: "))
更新:在其他用户和阅读的一些见解之后,我重写了代码并提出了这个:
class Game:
number_of_attempts = 3
no_more_attempts = "Game Over"
def attempt_down(self): #This will work as the counter of remaining lives.
self.number_of_attempts -= 1
print('Remaining Lives:',self.number_of_attempts)
def check_guess(self,letter):
"""
Requires
letter - a letter that has to be guessed
guess - a input from the user with the guessed letter
"""
while self.number_of_attempts > 0:
guess = input ("Guess the letter: ")
if guess.isalpha() == False:
print("Invalid!")
elif guess.lower() < letter:
self.attempt_down()
print("Low")
print("Try Again!")
elif guess.lower() > letter:
self.attempt_down()
print("High")
print("Try Again!")
elif guess.lower() == letter:
print("Correct!")
return True
print (self.no_more_attempts)
return False
game = Game()
"""
This is used to run the game.
Just insert the letter that
has to be guessed.
"""
teste1 = game.check_guess('g')
teste2 = game.check_guess('r')
慕后森
相关分类