# -*- coding: utf-8 -*-
"""Medium Project (2nd)-Khalid AlHarbi

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1h-FTdZ8LVHvzvAmV9__0amsoujIrzJWf
"""

import random

def choose_word():
    # List of words to choose from
    word_list = ["hangman", "python", "programming", "computer", "science", "algorithm", "keyboard", "mouse"]
    # Choose a random word from the list
    return random.choice(word_list)

def display_word(word, guessed_letters):
    # Display the word with placeholders for unguessed letters
    displayed_word = ""
    for letter in word:
        if letter in guessed_letters:
            displayed_word += letter + " "
        else:
            displayed_word += "_ "
    return displayed_word

def hangman():
    # Initialize variables
    word = choose_word()
    guessed_letters = []
    incorrect_guesses = 0
    max_guesses = 6

    print("Welcome to Hangman!")
    print(display_word(word, guessed_letters))

    while True:
        guess = input("Guess a letter: ").lower()

        if guess in guessed_letters:
            print("You already guessed that letter.")
            continue

        guessed_letters.append(guess)

        if guess in word:
            print("Correct!")
        else:
            print("Incorrect!")
            incorrect_guesses += 1

        print(display_word(word, guessed_letters))

        # Check if the player has won
        if all(letter in guessed_letters for letter in word):
            print("Congratulations! You guessed the word:", word)
            break

        # Check if the player has lost
        if incorrect_guesses == max_guesses:
            print("Sorry, you've run out of guesses. The word was:", word)
            break

if __name__ == "__main__":
    hangman()