Cod
Cod
Cod
utm_source=ig_web_copy_link
import random
def choose_word():
return random.choice(words)
display = ""
if letter in guessed_letters:
display += letter
else:
display += "_"
return display
def main():
word_to_guess = choose_word()
guessed_letters = set()
attempts = 6
if guess in guessed_letters:
guessed_letters.add(guess)
else:
guessed_letters.add(guess)
attempts -= 1
break
else:
if __name__ == "__main__":
main()
……………………………………………………………..
Explanation
1. choose_word Function:
o Selects a random word from a list of words.
2. display_word Function:
o Displays the word with guessed letters revealed and the rest as underscores.
3. main Function:
o Manages the game logic, keeps track of guessed letters, and the number of
attempts left.
o Informs the player if they have already guessed a letter, if their guess is correct, or
if their guess is incorrect.
o Ends the game if the player guesses the word or runs out of attempts.
Steps to Run
python guess_the_word.py
HANGMAN
import random
def choose_word():
return random.choice(words)
display = ""
if letter in guessed_letters:
else:
def main():
word_to_guess = choose_word()
guessed_letters = set()
attempts = 6
print("Welcome to Hangman!")
if guess in guessed_letters:
guessed_letters.add(guess)
else:
guessed_letters.add(guess)
attempts -= 1
break
else:
if __name__ == "__main__":
main()
………………………………………………………………………………………………………………
TIC TC TOE
def print_board(board):
print(" | ".join(row))
print("-" * 5)
def check_winner(board):
return row[0]
return board[0][col]
return board[0][0]
return board[0][2]
return None
def is_board_full(board):
return False
return True
def main():
current_player = "X"
while True:
print_board(board)
board[row][col] = current_player
winner = check_winner(board)
if winner:
print_board(board)
break
if is_board_full(board):
print_board(board)
print("It's a tie!")
break
else:
if __name__ == "__main__":
main()
…………………………………………………………………………………………………………….