为了账号安全,请及时绑定邮箱和手机立即绑定

猜猜计数控制

猜猜计数控制

潇潇雨雨 2022-12-14 20:29:21
我开始学习 Python,分配给我的任务是为一个猜谜游戏编写代码,在这个游戏中我必须控制玩家的总尝试次数。我已经描述了这些功能,它们正在工作(我相信......哈哈)但是当输入错误的猜测时我无法“重置”游戏......我写了这个:guess_count = []count_control = 1def 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 = 3no_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')
查看完整描述

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: "))


查看完整回答
反对 回复 2022-12-14
  • 1 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信