Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions Animal-Guess/animalguess.py
Original file line number Diff line number Diff line change
@@ -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()
Loading