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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy