0% found this document useful (0 votes)
34 views2 pages

Guess Game Code

Uploaded by

rehanriyaz3705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Guess Game Code

Uploaded by

rehanriyaz3705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

def choose_word():

words = ["python", "hangman", "programming", "challenge", "computer", "science", "learning"]

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 hangman():

print("Welcome to Hangman!")

secret_word = choose_word()

guessed_letters = []

attempts_left = 6

while True:

current_display = display_word(secret_word, guessed_letters)

print(f"Word: {current_display}")

print(f"Attempts left: {attempts_left}")

if "_" not in current_display:

print("Congratulations! You guessed the word!")

break

user_guess = input("Guess a letter: ").lower()


if user_guess in guessed_letters:

print("You already guessed that letter. Try again.")

elif user_guess in secret_word:

print("Good guess!")

guessed_letters.append(user_guess)

else:

print("Incorrect guess. Try again.")

guessed_letters.append(user_guess)

attempts_left -= 1

if attempts_left == 0:

print(f"Sorry, you ran out of attempts. The word was {secret_word}.")

break

if __name__ == "__main__":

hangman()

You might also like