Answers for "hangman project in python"

1

hangman project in python

import random
stages = ['''
  +---+
  |   |           You hanged the man
  |   |             ____________
  O   |            |  \/    \/  |
 /|\  |            |  /\    /\  |
 / \  |            |     *      |
                   |   ------   |
=========          |____________|
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']
words = ["in","apple","mango","good","india","priyanka","gramamr"]
computer_choice = random.choice(words)


display = []
word_length = len(computer_choice)
for _ in range(word_length):
    display += "_"
print(display)
The_end = False
lives = 6
while not The_end:
    if lives == 6:
        print(stages[6])
    guess = input("Guess a letter: ").lower()
    for position in range(word_length):
        letter = computer_choice[position]
        if letter == guess:
            display[position] = letter
        
    print(display)
    if guess not in computer_choice:
        lives -= 1
        if lives == 0:
            print("you lost")
            print(stages[0])
            The_end = True
        elif lives == 1:
            print(stages[1])
        elif lives == 2:
            print(stages[2])
        elif lives == 3:
            print(stages[3])
        elif lives == 4:
            print(stages[4])
        elif lives == 5:
            print(stages[5])
    if "_" not in display:
        The_end = True
        print("You won")
Posted by: Guest on June-26-2021
3

create a hangman game with python

#importing the time module
import time

#welcoming the user
name = raw_input("What is your name? ")

print "Hello, " + name, "Time to play hangman!"

print "
"

#wait for 1 second
time.sleep(1)

print "Start guessing..."
time.sleep(0.5)

#here we set the secret
word = "secret"

#creates an variable with an empty value
guesses = ''

#determine the number of turns
turns = 10

# Create a while loop

#check if the turns are more than zero
while turns > 0:         

    # make a counter that starts with zero
    failed = 0             

    # for every character in secret_word    
    for char in word:      

    # see if the character is in the players guess
        if char in guesses:    
    
        # print then out the character
            print char,    

        else:
    
        # if not found, print a dash
            print "_",     
       
        # and increase the failed counter with one
            failed += 1    

    # if failed is equal to zero

    # print You Won
    if failed == 0:        
        print "
You won"  

    # exit the script
        break              

    print

    # ask the user go guess a character
    guess = raw_input("guess a character:") 

    # set the players guess to guesses
    guesses += guess                    

    # if the guess is not found in the secret word
    if guess not in word:  
 
     # turns counter decreases with 1 (now 9)
        turns -= 1        
 
    # print wrong
        print "Wrong
"    
 
    # how many turns are left
        print "You have", + turns, 'more guesses' 
 
    # if the turns are equal to zero
        if turns == 0:           
    
        # print "You Lose"
            print "You Lose
"
Posted by: Guest on June-19-2020

Python Answers by Framework

Browse Popular Code Answers by Language