Skip to content

Commit 42ef4f7

Browse files
Enhanced Animal Guess Game with random questions and replay feature
1 parent 0e21fb2 commit 42ef4f7

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

Animal-Guess/animalguess.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
1+
import random
2+
13
def check_guess(guess, answer):
2-
global score
3-
still_guessing = True
4+
"""Check user's guess and return True if correct."""
45
attempt = 0
5-
while still_guessing and attempt < 3:
6+
while attempt < 3:
67
if guess.lower() == answer.lower():
7-
print("Correct Answer")
8-
score = score + 1
9-
still_guessing = False
8+
print("✅ Correct Answer!\n")
9+
return True
1010
else:
11-
if attempt < 2:
12-
guess = input("Sorry Wrong Answer, try again")
13-
attempt = attempt + 1
14-
if attempt == 3:
15-
print("The Correct answer is ",answer )
16-
17-
score = 0
18-
print("Guess the Animal")
19-
guess1 = input("Which bear lives at the North Pole? ")
20-
check_guess(guess1, "polar bear")
21-
guess2 = input("Which is the fastest land animal? ")
22-
check_guess(guess2, "Cheetah")
23-
guess3 = input("Which is the larget animal? ")
24-
check_guess(guess3, "Blue Whale")
25-
print("Your Score is "+ str(score))
11+
attempt += 1
12+
if attempt < 3:
13+
guess = input("❌ Wrong! Try again: ")
14+
print(f"The correct answer is: {answer}\n")
15+
return False
16+
17+
18+
def main():
19+
print("🐾 Welcome to the Animal Guessing Game! 🐾")
20+
print("You have 3 attempts for each question.\n")
21+
22+
questions = {
23+
"Which bear lives at the North Pole?": "polar bear",
24+
"Which is the fastest land animal?": "cheetah",
25+
"Which is the largest animal?": "blue whale",
26+
"Which animal is known as the king of the jungle?": "lion",
27+
"Which animal can sleep standing up?": "horse"
28+
}
29+
30+
score = 0
31+
# Randomize question order
32+
for question, answer in random.sample(list(questions.items()), 3):
33+
guess = input(question + " ")
34+
if check_guess(guess, answer):
35+
score += 1
36+
37+
print(f"🎯 Your final score is: {score}/{len(questions)}")
38+
39+
# Option to play again
40+
replay = input("\nDo you want to play again? (yes/no): ")
41+
if replay.lower().startswith('y'):
42+
print("\nRestarting game...\n")
43+
main()
44+
else:
45+
print("Thanks for playing! 🦋")
46+
47+
48+
if __name__ == "__main__":
49+
main()

0 commit comments

Comments
 (0)