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
25 changes: 18 additions & 7 deletions Animal-Guess/animalguess.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
def normalize_input(text):
"""
Normalize user input for comparison.
Strips whitespace and converts to lowercase.
"""
return text.strip().lower()

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

def check_guess(guess, answer):
"""
Expand All @@ -8,17 +13,20 @@ def check_guess(guess, answer):
Returns 1 if correct, 0 otherwise.
"""
attempt = 0

while attempt < 3:
if guess.lower().strip() == answer.lower():
if normalize_input(guess) == normalize_input(answer):
print("✅ Correct Answer!")
return 1 # increment score
return 1
else:
attempt += 1
if attempt < 3:
guess = input("❌ Wrong answer. Try again: ").strip()
guess = input("❌ Wrong answer. Try again: ")

print("The correct answer is:", answer)
return 0


def main():
"""
Main game function. Loops through all questions and calculates the total score.
Expand All @@ -33,12 +41,15 @@ def main():
score = 0

for question, answer in questions:
guess = input(question + " ").strip()
while not guess:
guess = input("Please enter a valid guess: ").strip()
guess = input(question + " ")

while not guess.strip():
guess = input("Please enter a valid guess: ")

score += check_guess(guess, answer)

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


if __name__ == "__main__":
main()