11import random
22
33def main ():
4+ # Cleaned up global variables (not strictly necessary unless modifying in nested scopes, but kept for your structure)
45 global all_guessed , attempts , correct_letters , display , guess , guessed_letters , hint , letter , max_attempts , selected , won , word , word_data , word_length
6+
57 print ("=" * 50 )
68 print ("WELCOME TO HANGMAN GAME" )
79 print ("=" * 50 )
@@ -17,49 +19,41 @@ def main():
1719 {"word" : "network" , "hint" : "Connected computers" },
1820 {"word" : "database" , "hint" : "Stores structured data" },
1921 {"word" : "algorithm" , "hint" : "Step-by-step problem-solving method" },
20-
2122 {"word" : "tiger" , "hint" : "Animal 🐾 with stripes" },
2223 {"word" : "elephant" , "hint" : "Largest land animal" },
2324 {"word" : "giraffe" , "hint" : "Tall animal with long neck" },
2425 {"word" : "lion" , "hint" : "King of jungle" },
2526 {"word" : "zebra" , "hint" : "Black and white striped animal" },
26-
2727 {"word" : "apple" , "hint" : "Fruit 🍎 keeps doctor away" },
2828 {"word" : "banana" , "hint" : "Yellow fruit 🍌" },
2929 {"word" : "mango" , "hint" : "King of fruits 🥭" },
3030 {"word" : "grapes" , "hint" : "Small round fruit 🍇" },
3131 {"word" : "orange" , "hint" : "Citrus fruit 🍊" },
32-
3332 {"word" : "india" , "hint" : "Country 🇮🇳 in South Asia" },
3433 {"word" : "china" , "hint" : "Most populated country" },
3534 {"word" : "brazil" , "hint" : "Country famous for Amazon rainforest" },
3635 {"word" : "canada" , "hint" : "Country with maple leaf 🍁" },
3736 {"word" : "japan" , "hint" : "Land of rising sun 🌅" },
38-
3937 {"word" : "school" , "hint" : "Place to study 📚" },
4038 {"word" : "teacher" , "hint" : "Person who teaches" },
4139 {"word" : "student" , "hint" : "Person who learns" },
4240 {"word" : "library" , "hint" : "Place with books" },
4341 {"word" : "college" , "hint" : "Higher education institute" },
44-
4542 {"word" : "football" , "hint" : "Sport ⚽ played worldwide" },
4643 {"word" : "cricket" , "hint" : "Popular sport in India 🏏" },
4744 {"word" : "tennis" , "hint" : "Played with racket 🎾" },
4845 {"word" : "hockey" , "hint" : "India's national sport" },
4946 {"word" : "badminton" , "hint" : "Played with shuttlecock" },
50-
5147 {"word" : "doctor" , "hint" : "Treats patients 🩺" },
5248 {"word" : "engineer" , "hint" : "Builds and designs systems" },
5349 {"word" : "artist" , "hint" : "Creates paintings 🎨" },
5450 {"word" : "lawyer" , "hint" : "Works with law ⚖️" },
5551 {"word" : "chef" , "hint" : "Cooks food 👨🍳" },
56-
5752 {"word" : "mobile" , "hint" : "Used for calling 📱" },
5853 {"word" : "laptop" , "hint" : "Portable computer 💻" },
5954 {"word" : "camera" , "hint" : "Used to take photos 📷" },
6055 {"word" : "speaker" , "hint" : "Outputs sound 🔊" },
6156 {"word" : "battery" , "hint" : "Stores power 🔋" },
62-
6357 {"word" : "rain" , "hint" : "Water falling from sky 🌧️" },
6458 {"word" : "summer" , "hint" : "Hot season ☀️" },
6559 {"word" : "winter" , "hint" : "Cold season ❄️" },
@@ -70,7 +64,6 @@ def main():
7064 selected = random .choice (word_data )
7165 word = selected ["word" ]
7266 hint = selected ["hint" ]
73-
7467 word_length = len (word )
7568
7669 guessed_letters = []
@@ -84,6 +77,7 @@ def main():
8477 print (f"You have { max_attempts } attempts to guess the word.\n " )
8578
8679 while attempts < max_attempts and not won :
80+ # 1. Update and show the current word progress
8781 display = ""
8882 for letter in word :
8983 if letter in correct_letters :
@@ -92,49 +86,49 @@ def main():
9286 display += "_ "
9387
9488 print (f"Word: { display } " )
89+
90+ # 2. CHECK WIN CONDITION HERE BEFORE ASKING FOR A NEW GUESS
91+ # If there are no more underscores left, the player has won!
92+ if "_" not in display :
93+ won = True
94+ break
95+
9596 print (f"Attempts remaining: { max_attempts - attempts } " )
9697 print (f"Guessed letters: { ', ' .join (guessed_letters ) if guessed_letters else 'None' } " )
9798
9899 guess = input ("\n Guess a letter: " ).lower ()
99100
100101 if len (guess ) != 1 or not guess .isalpha ():
101102 print ("Please enter a single letter!" )
103+ print ("-" * 50 )
102104 continue
103105
104106 if guess in guessed_letters :
105107 print ("You already guessed that letter!" )
108+ print ("-" * 50 )
106109 continue
107110
108111 guessed_letters .append (guess )
109112
110113 if guess in word :
111114 print (f"✓ Correct! '{ guess } ' is in the word." )
112115 correct_letters .append (guess )
113-
114- all_guessed = True
115- for letter in word :
116- if letter not in correct_letters :
117- all_guessed = False
118- break
119-
120- if all_guessed :
121- won = True
122116 else :
123117 print (f"✗ Wrong! '{ guess } ' is not in the word." )
124118 attempts += 1
125119
126120 print ("-" * 50 )
127121
122+ # Endgame sequence
128123 print ("\n " + "=" * 50 )
129124 if won :
130- print ("🎉 CONGRATULATIONS! YOU WON!" )
125+ print ("🎉 CONGRATULATIONS! YOU WON LIGHTNING FAST !" )
131126 print (f"The word was: { word } " )
132127 print (f"You guessed it with { max_attempts - attempts } attempts remaining!" )
133128 else :
134129 print ("😔 GAME OVER! YOU LOST!" )
135130 print (f"The word was: { word } " )
136131 print ("=" * 50 )
137132
138-
139133if __name__ == '__main__' :
140- main ()
134+ main ()
0 commit comments