diff --git a/Animal-Guess/animalguess.py b/Animal-Guess/animalguess.py index 6dc3fe4..62dbd77 100644 --- a/Animal-Guess/animalguess.py +++ b/Animal-Guess/animalguess.py @@ -1,25 +1,44 @@ + +# Improved version with modular structure, input validation, and typo fixes + def check_guess(guess, answer): - global score - still_guessing = True + """ + Checks the user's guess against the correct answer. + Allows up to 3 attempts. + Returns 1 if correct, 0 otherwise. + """ attempt = 0 - while still_guessing and attempt < 3: - if guess.lower() == answer.lower(): - print("Correct Answer") - score = score + 1 - still_guessing = False + while attempt < 3: + if guess.lower().strip() == answer.lower(): + print("āœ… Correct Answer!") + return 1 # increment score else: - if attempt < 2: - guess = input("Sorry Wrong Answer, try again") - attempt = attempt + 1 - if attempt == 3: - print("The Correct answer is ",answer ) - -score = 0 -print("Guess the Animal") -guess1 = input("Which bear lives at the North Pole? ") -check_guess(guess1, "polar bear") -guess2 = input("Which is the fastest land animal? ") -check_guess(guess2, "Cheetah") -guess3 = input("Which is the larget animal? ") -check_guess(guess3, "Blue Whale") -print("Your Score is "+ str(score)) + attempt += 1 + if attempt < 3: + guess = input("āŒ Wrong answer. Try again: ").strip() + print("The correct answer is:", answer) + return 0 + +def main(): + """ + Main game function. Loops through all questions and calculates the total score. + """ + questions = [ + ("Which bear lives at the North Pole?", "polar bear"), + ("Which is the fastest land animal?", "cheetah"), + ("Which is the largest animal?", "blue whale") + ] + + print("🦁 Welcome to 'Guess the Animal' Game! 🐘") + score = 0 + + for question, answer in questions: + guess = input(question + " ").strip() + while not guess: + guess = input("Please enter a valid guess: ").strip() + score += check_guess(guess, answer) + + print(f"\nšŸ† Your Total Score is: {score} out of {len(questions)}") + +if __name__ == "__main__": + main()