猜猜计数控制

我开始学习 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')


潇潇雨雨
浏览 73回答 1
1回答

慕后森

问题在于您有一个游戏状态,您正在将其作为全局变量进行跟踪,guess_count并且count_control这是为什么 python 和其他语言提供类和对象的示例:class Game:&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; self.guess_count = []&nbsp; &nbsp; &nbsp; &nbsp; self.count_control = 1&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def check_guess(letter, guess):&nbsp; &nbsp; &nbsp; &nbsp; if guess.isalpha() == False:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Invalid!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp; &nbsp; &nbsp; &nbsp; elif guess.lower() < letter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Low")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp; &nbsp; &nbsp; &nbsp; elif guess.lower() > letter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("High")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp; &nbsp; &nbsp; &nbsp; elif guess.lower() == letter:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Correct!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("anything")&nbsp; &nbsp; def letter_guess(self, guess):&nbsp; &nbsp; &nbsp; &nbsp; self.check_guess('a', guess)&nbsp; &nbsp; &nbsp; &nbsp; while len(self.guess_count) <= 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.check_guess('a', guess) == True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif self.check_guess('a', guess) == False:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.guess_count.append(self.count_control)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guess = input("Try again \n")game = Game()game.letter_guess(input("test: "))game = Game()game.letter_guess(input("test: "))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python