Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

hangman in python

#simple version of hangman without the drawings 
#and made with an API for the random words

import requests

# variables
word = requests.get("https://random-word-api.herokuapp.com/word").json()[0]
string = ""
positionOfLetter = []
tries = 10

# create string from dashes
for letters in word:
    string = string + "_"
print("
The word is...")
print(string)


# the game
while "_" in string and tries > 0:
    guess = input("
Guess the a letter: ")
    if guess in word:
        for pos, char in enumerate(word):
            if(char == guess):
                positionOfLetter.append(pos)
        for position in positionOfLetter:
            string = string[:position] + guess + string[position+1:]
        print("That letter IS in the word!, you have " + str(tries) + " lifes left!")
    else:
        print("That letter is not in the word")
        tries -= 1
    positionOfLetter = []
    
    print(string)
    

# end of the game, the word is guessed
print("
you finished the game with " + str(tries) + " lifes left!, the word was:
" + "33[1m" + word + "33[1m")
Comment

hangman python

while(tries > 0):
        #variable to count number of incorrect attempts
            incorrect = 0

            for letter in word:     
                        if letter in totalGuesses:
                        #print the correctly guessed letter if found
                                    print(letter)
                        else:
                        #print a dash if letter not in word
                                    print("_")
                                    incorrect += 1

            if incorrect == 0:
                        #player wins
                        print("You Win")

                        print("Word is - ", word)
                        break

            #input again if wrong letter guessed
            guess = input("
Guess a letter in the word - ")

            #store guessed letters in totalGuesses
            totalGuesses += guess

            #if guess in not present in the word
            if guess not in word:               
                        tries -= 1
                        print("Wrong")

                        #print tries left for the player
                        print("You have ", + tries, 'guesses left')


                        if tries == 0:
                                    print("You Lose")
Comment

python hangman

import random as rd

word_bank = {'Person name': ['william', 'peter', 'wallage'],
             'car': ['bmw', 'mercades', 'toyota']}
word_bank_category = ['Person name', 'car']

category_name = word_bank_category[rd.randint(0, 1)] # display the category name
item_index = rd.randint(0, 2) # display the selection item in each category
correct_word = str(word_bank[category_name][item_index])

correct_word_list = list(correct_word) # check up with user's input
display_list = [char.replace(char, '_') for char in correct_word] # displaying to the users
print('%s: %s(%d alphabets)' % (category_name, display_list, len(display_list)))

wrong_guesses = []
limit_guesses = 0
count = 0

def result():
    if '_' not in display_list:
        print('win!'), quit()

def hangman_drawer():
    global limit_guesses
    if limit_guesses == 1:
        print("   _____ 
"
              "  |      
"
              "  |      
"
              "  |      
"
              "  |      
"
              "  |      
"
              "  |      
"
              "__|__
")
        print("Wrong guess. " + str(limit_guesses) + " guesses remaining
")
    elif limit_guesses == 2:
        print("   _____ 
"
              "  |     | 
"
              "  |     |
"
              "  |     | 
"
              "  |     O 
"
              "  |      
"
              "  |      
"
              "__|__
")
        print("Wrong guess. " + str(limit_guesses) + " guesses remaining
")
    elif limit_guesses == 3:
        print("   _____ 
"
              "  |     | 
"
              "  |     |
"
              "  |     | 
"
              "  |     O 
"
              "  |    /  
"
              "  |      
"
              "__|__
")
        print("Wrong guess. " + str(limit_guesses) + " guesses remaining
")
    elif limit_guesses == 4:
        print("   _____ 
"
              "  |     | 
"
              "  |     |
"
              "  |     | 
"
              "  |     O 
"
              "  |    /| 
"
              "  |      
"
              "__|__
")
        print("Wrong guess. " + str(limit_guesses) + " last guess remaining
")
    elif limit_guesses == 5:
        print("   _____ 
"
              "  |     | 
"
              "  |     |
"
              "  |     | 
"
              "  |     O 
"
              "  |    /| 
"
              "  |    /  
"
              "__|__
")
        print("Wrong guess. You are hanged!!!
")
        print('The word was: "%s"' % correct_word)

def user_input():
    global limit_guesses, count
    while True:
        print('wrong guesses:',wrong_guesses)
        player_guess = input('Please enter your guess(alphabet): ')
        if player_guess in correct_word_list:
            count += 1
            guess_index = correct_word_list.index(player_guess) # finding the index of the correct alphabet
            correct_word_list[guess_index] = '-'
            display_list[guess_index] = player_guess # replace the dash with the correct alphabet user had input
            print('%s: %s(%d alphabets left)' % (category_name, display_list, len(display_list) - count))
            result()
        else:
            wrong_guesses.append(player_guess)
            limit_guesses += 1
            hangman_drawer()
            result()

user_input()
Comment

python hangman

import random as rd

word_bank = {'Person name': ['william', 'peter', 'wallage'],
             'car': ['bmw', 'mercades', 'toyota']}
word_bank_category = ['Person name', 'car']

category_name = word_bank_category[rd.randint(0, 1)] # display the category name
item_index = rd.randint(0, 2) # display the selection item in each category
correct_word = str(word_bank[category_name][item_index])

correct_word_list = list(correct_word) # check up with user's input
display_list = [char.replace(char, '_') for char in correct_word] # displaying to the users
print('%s: %s(%d alphabets)' % (category_name, display_list, len(display_list)))

wrong_guesses = []
limit_guesses = 0
count = 0

def result():
    if '_' not in display_list:
        print('win!'), quit()

def hangman_drawer():
    global limit_guesses
    if limit_guesses == 1:
        print("   _____ 
"
              "  |      
"
              "  |      
"
              "  |      
"
              "  |      
"
              "  |      
"
              "  |      
"
              "__|__
")
        print("Wrong guess. " + str(limit_guesses) + " guesses remaining
")
    elif limit_guesses == 2:
        print("   _____ 
"
              "  |     | 
"
              "  |     |
"
              "  |     | 
"
              "  |     O 
"
              "  |      
"
              "  |      
"
              "__|__
")
        print("Wrong guess. " + str(limit_guesses) + " guesses remaining
")
    elif limit_guesses == 3:
        print("   _____ 
"
              "  |     | 
"
              "  |     |
"
              "  |     | 
"
              "  |     O 
"
              "  |    /  
"
              "  |      
"
              "__|__
")
        print("Wrong guess. " + str(limit_guesses) + " guesses remaining
")
    elif limit_guesses == 4:
        print("   _____ 
"
              "  |     | 
"
              "  |     |
"
              "  |     | 
"
              "  |     O 
"
              "  |    /| 
"
              "  |      
"
              "__|__
")
        print("Wrong guess. " + str(limit_guesses) + " last guess remaining
")
    elif limit_guesses == 5:
        print("   _____ 
"
              "  |     | 
"
              "  |     |
"
              "  |     | 
"
              "  |     O 
"
              "  |    /| 
"
              "  |    /  
"
              "__|__
")
        print("Wrong guess. You are hanged!!!
")
        print('The word was: "%s"' % correct_word)

def user_input():
    global limit_guesses, count
    while True:
        print('wrong guesses:',wrong_guesses)
        player_guess = input('Please enter your guess(alphabet): ')
        if player_guess in correct_word_list:
            count += 1
            guess_index = correct_word_list.index(player_guess) # finding the index of the correct alphabet
            correct_word_list[guess_index] = '-'
            display_list[guess_index] = player_guess # replace the dash with the correct alphabet user had input
            print('%s: %s(%d alphabets left)' % (category_name, display_list, len(display_list) - count))
            result()
        else:
            wrong_guesses.append(player_guess)
            limit_guesses += 1
            hangman_drawer()
            result()

user_input()
Comment

PREVIOUS NEXT
Code Example
Python :: height and width of colorbar 
Python :: jouer à Snake 
Python :: inicair venv python 
Python :: python pool 
Python :: get derivative of interp1d 
Python :: iterate through keys in dictionary 
Python :: how to make a number guessing game in python 
Python :: get a liste from a txt file python 
Python :: can i save additional information with png file 
Python :: python list comprehension with filter example 2 
Python :: k7yKJk8vdjHvw56q7bCTxibvT 
Python :: Now, we will first look at the simplest way to scan ports with Python 
Python :: number of features classification model jupyter notebook 
Python :: Display all resources in table pandas 
Python :: pandas get cvvlaue from antoiher column fom one coluikmnn value 
Python :: Inpunt and output 
Python :: colab not training always giving cuda out of memory error eventhough memory is available 
Python :: sublime python build system 
Python :: load data batchwise keras 
Python :: MyTestCase 
Python :: Grid-Strategy 
Python :: creating a record in python 
Python :: The Model display 
Python :: Sort list in-place (Original list is modified) 
Python :: duplicate a list with lowercase in python 
Python :: python arithmetic operation with list 
Python :: programme phyton pour realiser un programme qui transforme une image en niveau de gris 
Python :: python How do I remove the dots / noise without damaging the text? 
Python :: Parsing a url for IP address using python 
Python :: python check column conditions 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =