Skip to content

Commit 7f99f80

Browse files
authored
PR #69: Refractored Animal Guess
Refactor input comparison logic for better readability Merge pull request #69 from Tithi234/patch-1
2 parents 80614c5 + 47dd984 commit 7f99f80

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

Animal-Guess/animalguess.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
def normalize_input(text):
2+
"""
3+
Normalize user input for comparison.
4+
Strips whitespace and converts to lowercase.
5+
"""
6+
return text.strip().lower()
17

2-
# Improved version with modular structure, input validation, and typo fixes
38

49
def check_guess(guess, answer):
510
"""
@@ -8,17 +13,20 @@ def check_guess(guess, answer):
813
Returns 1 if correct, 0 otherwise.
914
"""
1015
attempt = 0
16+
1117
while attempt < 3:
12-
if guess.lower().strip() == answer.lower():
18+
if normalize_input(guess) == normalize_input(answer):
1319
print("✅ Correct Answer!")
14-
return 1 # increment score
20+
return 1
1521
else:
1622
attempt += 1
1723
if attempt < 3:
18-
guess = input("❌ Wrong answer. Try again: ").strip()
24+
guess = input("❌ Wrong answer. Try again: ")
25+
1926
print("The correct answer is:", answer)
2027
return 0
2128

29+
2230
def main():
2331
"""
2432
Main game function. Loops through all questions and calculates the total score.
@@ -33,12 +41,15 @@ def main():
3341
score = 0
3442

3543
for question, answer in questions:
36-
guess = input(question + " ").strip()
37-
while not guess:
38-
guess = input("Please enter a valid guess: ").strip()
44+
guess = input(question + " ")
45+
46+
while not guess.strip():
47+
guess = input("Please enter a valid guess: ")
48+
3949
score += check_guess(guess, answer)
4050

4151
print(f"\n🏆 Your Total Score is: {score} out of {len(questions)}")
4252

53+
4354
if __name__ == "__main__":
4455
main()

0 commit comments

Comments
 (0)