https://www.instagram.com/reel/C8J3VTbInuV/?
utm_source=ig_web_copy_link
GUESS THE WORD
import random
def choose_word():
words = ["python", "java", "swift", "kotlin", "javascript", "ruby"]
return random.choice(words)
def display_word(word, guessed_letters):
display = ""
for letter in word:
if letter in guessed_letters:
display += letter
else:
display += "_"
return display
def main():
word_to_guess = choose_word()
guessed_letters = set()
attempts = 6
print("Welcome to Guess the Word!")
print("You have 6 attempts to guess the word correctly.")
while attempts > 0:
print("\nWord to guess:", display_word(word_to_guess, guessed_letters))
guess = input("Enter a letter: ").lower()
if guess in guessed_letters:
print("You already guessed that letter. Try again.")
elif guess in word_to_guess:
guessed_letters.add(guess)
print("Good job! That letter is in the word.")
else:
guessed_letters.add(guess)
attempts -= 1
print(f"Incorrect guess. You have {attempts} attempts left.")
if all(letter in guessed_letters for letter in word_to_guess):
print("\nCongratulations! You guessed the word:", word_to_guess)
break
else:
print("\nSorry, you've run out of attempts. The word was:", word_to_guess)
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
1. Ensure Python is installed on your system.
2. Copy the code into a file, for example, guess_the_word.py.
3. Run the game by executing the following command in your terminal:
python guess_the_word.py
HANGMAN
import random
def choose_word():
words = ["python", "java", "swift", "kotlin", "javascript", "ruby"]
return random.choice(words)
def display_word(word, guessed_letters):
display = ""
for letter in word:
if letter in guessed_letters:
display += letter + " "
else:
display += "_ "
return display.strip()
def main():
word_to_guess = choose_word()
guessed_letters = set()
attempts = 6
print("Welcome to Hangman!")
print("You have 6 attempts to guess the word correctly.")
while attempts > 0:
print("\nWord to guess:", display_word(word_to_guess, guessed_letters))
guess = input("Enter a letter: ").lower()
if guess in guessed_letters:
print("You already guessed that letter. Try again.")
elif guess in word_to_guess:
guessed_letters.add(guess)
print("Good job! That letter is in the word.")
else:
guessed_letters.add(guess)
attempts -= 1
print(f"Incorrect guess. You have {attempts} attempts left.")
if all(letter in guessed_letters for letter in word_to_guess):
print("\nCongratulations! You guessed the word:", word_to_guess)
break
else:
print("\nSorry, you've run out of attempts. The word was:", word_to_guess)
if __name__ == "__main__":
main()
………………………………………………………………………………………………………………
TIC TC TOE
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 5)
def check_winner(board):
for row in board:
if row[0] == row[1] == row[2] and row[0] != " ":
return row[0]
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != " ":
return board[0][col]
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != " ":
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != " ":
return board[0][2]
return None
def is_board_full(board):
for row in board:
if " " in row:
return False
return True
def main():
board = [[" " for _ in range(3)] for _ in range(3)]
current_player = "X"
while True:
print_board(board)
row = int(input(f"Player {current_player}, enter the row (0-2): "))
col = int(input(f"Player {current_player}, enter the column (0-2): "))
if board[row][col] == " ":
board[row][col] = current_player
winner = check_winner(board)
if winner:
print_board(board)
print(f"Player {winner} wins!")
break
if is_board_full(board):
print_board(board)
print("It's a tie!")
break
current_player = "O" if current_player == "X" else "X"
else:
print("Cell already taken, try again.")
if __name__ == "__main__":
main()
…………………………………………………………………………………………………………….