0% found this document useful (0 votes)
5 views

coding (hangman)

This document contains a Python implementation of the Hangman game. Players guess letters to reveal a hidden word, with a limited number of incorrect attempts allowed. The game provides feedback on guesses and displays the current state of the word being guessed.

Uploaded by

sudasinghesan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

coding (hangman)

This document contains a Python implementation of the Hangman game. Players guess letters to reveal a hidden word, with a limited number of incorrect attempts allowed. The game provides feedback on guesses and displays the current state of the word being guessed.

Uploaded by

sudasinghesan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

#List of words for the game


word_list=["python","hangman","programming","computer","keyboard","developer","learning"]

def choose_word():
return random.choice(word_list)

def display_word(word,guessed_letters):
display=""
for letter in word:
if letter in guessed_letters:
display += letter
else:
display+="_"
return display

def main():
print("Welcome to Hangman!")
word_to_guess = choose_word()
guessed_letters=[]
attempts=6 #Number of attempts allowed

while attempts >0:


print("/nAttempts left:",attempts)
current_display=display_word(word_to_guess,guessed_letters)

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

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

if guess in word_to_guess:
guessed_letters.append(guess)
if current_display == word_to_guess:
print("Congratulations! You guessed the word:",word_to_guess)
else:
guessed_letters.append(guess)
attempts -=1
print("Incorrect guess. Try again.")

if attempts ==0:
print("You're out of attempts. The word was:",word_to_guess)

if __name__=="__main__":
main()

Welcome to Hangman!
/nAttempts left: 6
Guess a letter:a
Incorrect guess. Try again.
/nAttempts left: 5
Guess a letter:7
Incorrect guess. Try again.
/nAttempts left: 4
Guess a letter:g
Incorrect guess. Try again.
/nAttempts left: 3
Guess a letter:d
Incorrect guess. Try again.
/nAttempts left: 2
Guess a letter:m
Incorrect guess. Try again.
/nAttempts left: 1
Guess a letter:g
You already guessed that letter
/nAttempts left: 1
Guess a letter:a
You already guessed that letter
/nAttempts left: 1
Guess a letter:h
/nAttempts left: 1
Guess a letter:f
Incorrect guess. Try again.
You're out of attempts. The word was: python
>

You might also like