@@ -61,74 +61,80 @@ def main():
6161 {"word" : "storm" , "hint" : "Strong wind and rain" }
6262 ]
6363
64- selected = random .choice (word_data )
65- word = selected ["word" ]
66- hint = selected ["hint" ]
67- word_length = len (word )
64+ while True :
65+ selected = random .choice (word_data )
66+ word = selected ["word" ]
67+ hint = selected ["hint" ]
68+ word_length = len (word )
6869
69- guessed_letters = []
70- correct_letters = []
71- max_attempts = 6
72- attempts = 0
73- won = False
70+ guessed_letters = []
71+ correct_letters = []
72+ max_attempts = 6
73+ attempts = 0
74+ won = False
7475
75- print (f"\n The word has { word_length } letters." )
76- print (f"Hint: { hint } " )
77- print (f"You have { max_attempts } attempts to guess the word.\n " )
76+ print (f"\n The word has { word_length } letters." )
77+ print (f"Hint: { hint } " )
78+ print (f"You have { max_attempts } attempts to guess the word.\n " )
7879
79- while attempts < max_attempts and not won :
80- # 1. Update and show the current word progress
81- display = ""
82- for letter in word :
83- if letter in correct_letters :
84- display += letter + " "
85- else :
86- display += "_ "
87-
88- print (f"Word: { display } " )
80+ while attempts < max_attempts and not won :
81+ # 1. Update and show the current word progress
82+ display = ""
83+ for letter in word :
84+ if letter in correct_letters :
85+ display += letter + " "
86+ else :
87+ display += "_ "
8988
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
89+ print (f"Word: { display } " )
90+
91+ # 2. CHECK WIN CONDITION HERE BEFORE ASKING FOR A NEW GUESS
92+ # If there are no more underscores left, the player has won!
93+ if "_" not in display :
94+ won = True
95+ break
9596
96- print (f"Attempts remaining: { max_attempts - attempts } " )
97- print (f"Guessed letters: { ', ' .join (guessed_letters ) if guessed_letters else 'None' } " )
98-
99- guess = input ("\n Guess a letter: " ).lower ()
100-
101- if len (guess ) != 1 or not guess .isalpha ():
102- print ("Please enter a single letter!" )
103- print ("-" * 50 )
104- continue
105-
106- if guess in guessed_letters :
107- print ("You already guessed that letter!" )
97+ print (f"Attempts remaining: { max_attempts - attempts } " )
98+ print (f"Guessed letters: { ', ' .join (guessed_letters ) if guessed_letters else 'None' } " )
99+
100+ guess = input ("\n Guess a letter: " ).lower ()
101+
102+ if len (guess ) != 1 or not guess .isalpha ():
103+ print ("Please enter a single letter!" )
104+ print ("-" * 50 )
105+ continue
106+
107+ if guess in guessed_letters :
108+ print ("You already guessed that letter!" )
109+ print ("-" * 50 )
110+ continue
111+
112+ guessed_letters .append (guess )
113+
114+ if guess in word :
115+ print (f"✓ Correct! '{ guess } ' is in the word." )
116+ correct_letters .append (guess )
117+ else :
118+ print (f"✗ Wrong! '{ guess } ' is not in the word." )
119+ attempts += 1
120+
108121 print ("-" * 50 )
109- continue
110-
111- guessed_letters . append ( guess )
112-
113- if guess in word :
114- print (f"✓ Correct! ' { guess } ' is in the word. " )
115- correct_letters . append ( guess )
122+
123+ # Endgame sequence
124+ print ( " \n " + "=" * 50 )
125+ if won :
126+ print ( "🎉 CONGRATULATIONS! YOU WON LIGHTNING FAST!" )
127+ print (f"The word was: { word } " )
128+ print ( f"You guessed it with { max_attempts - attempts } attempts remaining!" )
116129 else :
117- print (f"✗ Wrong! '{ guess } ' is not in the word." )
118- attempts += 1
119-
120- print ("-" * 50 )
130+ print ("😔 GAME OVER! YOU LOST!" )
131+ print (f"The word was: { word } " )
132+ print ("=" * 50 )
121133
122- # Endgame sequence
123- print ("\n " + "=" * 50 )
124- if won :
125- print ("🎉 CONGRATULATIONS! YOU WON LIGHTNING FAST!" )
126- print (f"The word was: { word } " )
127- print (f"You guessed it with { max_attempts - attempts } attempts remaining!" )
128- else :
129- print ("😔 GAME OVER! YOU LOST!" )
130- print (f"The word was: { word } " )
131- print ("=" * 50 )
134+ play_again = input ("\n 🔄 Do you want to play again? (y/n): " ).strip ().lower ()
135+ if play_again not in ['y' , 'yes' ]:
136+ print ("\n 👋 Thanks for playing Hangman! Goodbye!\n " )
137+ break
132138
133139if __name__ == '__main__' :
134140 main ()
0 commit comments