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
49def 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+
2230def 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+
4354if __name__ == "__main__" :
4455 main ()
0 commit comments