Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
792 B
1
Indexable
Never
import random

def play_hangman():
    print("Welcome to Hangman!")
    words = ["python", "java", "ruby", "javascript", "php", "swift"]
    word = random.choice(words)
    guesses = ""
    turns = 6

    while turns > 0:
        failed = 0
        for char in word:
            if char in guesses:
                print(char, end=" ")
            else:
                print("_", end=" ")
                failed += 1
        if failed == 0:
            print("\nYou won!")
            break
        print("\n")
        guess = input("Guess a letter: ")
        guesses += guess
        if guess not in word:
            turns -= 1
            print(f"Wrong! You have {turns} turns left.")
            if turns == 0:
                print(f"\nYou lose! The word was {word}.")

play_hangman()