|
| 1 | +import random |
| 2 | + |
1 | 3 | def check_guess(guess, answer): |
2 | | - global score |
3 | | - still_guessing = True |
| 4 | + """Check user's guess and return True if correct.""" |
4 | 5 | attempt = 0 |
5 | | - while still_guessing and attempt < 3: |
| 6 | + while attempt < 3: |
6 | 7 | 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 |
10 | 10 | 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