Skip to content

Commit fb7e665

Browse files
authored
PR #56: Improve Animal Guess
Improve animalguess.py: modularize, fix typos, remove globals, add in… Merge pull request #56 from Achi-Vyshnavi/fix-bugs-python-scripts
2 parents 78feb45 + 88643fa commit fb7e665

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed

Animal-Guess/animalguess.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
1+
2+
# Improved version with modular structure, input validation, and typo fixes
3+
14
def check_guess(guess, answer):
2-
global score
3-
still_guessing = True
5+
"""
6+
Checks the user's guess against the correct answer.
7+
Allows up to 3 attempts.
8+
Returns 1 if correct, 0 otherwise.
9+
"""
410
attempt = 0
5-
while still_guessing and attempt < 3:
6-
if guess.lower() == answer.lower():
7-
print("Correct Answer")
8-
score = score + 1
9-
still_guessing = False
11+
while attempt < 3:
12+
if guess.lower().strip() == answer.lower():
13+
print("✅ Correct Answer!")
14+
return 1 # increment score
1015
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))
16+
attempt += 1
17+
if attempt < 3:
18+
guess = input("❌ Wrong answer. Try again: ").strip()
19+
print("The correct answer is:", answer)
20+
return 0
21+
22+
def main():
23+
"""
24+
Main game function. Loops through all questions and calculates the total score.
25+
"""
26+
questions = [
27+
("Which bear lives at the North Pole?", "polar bear"),
28+
("Which is the fastest land animal?", "cheetah"),
29+
("Which is the largest animal?", "blue whale")
30+
]
31+
32+
print("🦁 Welcome to 'Guess the Animal' Game! 🐘")
33+
score = 0
34+
35+
for question, answer in questions:
36+
guess = input(question + " ").strip()
37+
while not guess:
38+
guess = input("Please enter a valid guess: ").strip()
39+
score += check_guess(guess, answer)
40+
41+
print(f"\n🏆 Your Total Score is: {score} out of {len(questions)}")
42+
43+
if __name__ == "__main__":
44+
main()

0 commit comments

Comments
 (0)