def hangman(): secret_word = choose_word() guessmade = "" turns = len(secret_word) print("len of the word", len(secret_word)) while len(secret_word) >= 0: main_word = "" for letter in secret_word: if letter in guessmade: main_word += letter else: main_word += "_ " if main_word == secret_word: print(main_word) print("YOU WIN!", "\U0001F929") break print("guess the word", main_word) guess = input("=") if guess in secret_word: guessmade += guess else: print("enter valid letter") if guess not in secret_word: turns -= 1 if turns == len(secret_word) - 1: print(len(secret_word) - 1, "turns are left") print("--------------------") print(IMAGES[0], ) if turns == len(secret_word) - 2: print(len(secret_word) - 2, "turns are left") print("--------------------") print(IMAGES[1]) if turns == len(secret_word) - 3: print(len(secret_word) - 3, "turns are left") print("--------------------") print(IMAGES[2]) if turns == len(secret_word) - 4: print(len(secret_word) - 4, "turns are left") print("--------------------") print(IMAGES[3]) if turns == len(secret_word) - 5: print(len(secret_word) - 5, "turns are left") print(IMAGES[4]) if turns == len(secret_word) - 6: print(len(secret_word) - 6, "turns are left") print("--------------------") print(IMAGES[5]) if turns == len(secret_word) - 7: print( len(secret_word) - 7, "turns are left Hangman on his last breath") print("--------------------") print(IMAGES[6]) if turns == len(secret_word) - 8: print("you loose") print("--------------------") print(IMAGES[7]) print("The word was", secret_word, "Better Luck Next Time:(") break
def play_hangman(): #Play a game of hangman print('Starting a game of Hangman...') attempts_remaining = get_number_attempts() #print(attempts_remaining) word_length = get_word_length() #Randomly select a word print('Selecting a word...') word = choose_word(word_length) print() check = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) #print(remaining_letters) wrong_letters = [] word_solved = False #Main game loop while attempts_remaining > 0 and not word_solved: #print current game state print("Word:{}".format(get_display_word(word, check))) print("Attempts Remaining:{}".format(attempts_remaining)) print("Previous Guesses:{}".format(" ".join(wrong_letters))) #Get player's next letter guess next_letter = get_next_letter(remaining_letters) #check if letter guess is in word if next_letter in word: #Guessed correctly print("{} is in the word!".format(next_letter)) #Reveal matching letters for i in range(len(word)): if word[i] == next_letter: check[i] = True else: #Guessed incorrectly print("{} is not in the word!".format(next_letter)) #Decrement number of attempts left and append guess to wrong guesses attempts_remaining -= 1 wrong_letters.append(next_letter) #Check if word is completely solved if False not in check: word_solved = True print() #The game is over: reveal the word print("The word is {}".format(word)) #Notify player of victory or defeat if word_solved: print("Congratulations! You won!") else: print("Try again next time!") #Ask player if he/she wants to try again try_again = input("Would you like play again?[y/Y]") return try_again.lower() == 'y'
print "Good guess: " + get_guessed_word(secret_word, letters_guessed) print "" if is_word_guessed(secret_word, letters_guessed) == True: print " * * Congratulations, you won! * * " print "" break else: print "Oops! That letter is not in my word: " + get_guessed_word( secret_word, letters_guessed) print IMAGES[8 - remaining_lives] letters_guessed.append(letter) print "" if y == "easy": remaining_lives -= 1 elif y == "medium": remaining_lives -= 2 elif y == "hard": remaining_lives -= 3 else: print "oombi pundachi mona crt ah type pannu la thevidiya" print "The secret word is " + secret_word # Load the list of words into the variable wordlist # So that it can be accessed from anywhere in the program secret_word = choose_word() hangman(secret_word)
while True: available_letters = get_available_letters(letters_guessed) print("Available letters: {} ".format(available_letters)) guess = input("Please guess a letter: ") letter = guess.lower() if letter in secret_word: letters_guessed.append(letter) print("Good guess: {} \n".format( get_guessed_word(secret_word, letters_guessed))) if is_word_guessed(secret_word, letters_guessed): print(" * * Congratulations, you won! * * ", end='\n\n') break else: print("Oops! That letter is not in my word.") if wrongAttempts < len(IMAGES) - 1: print("You are going to be hung.") print(IMAGES[wrongAttempts]) else: print("Alas!! You are hung !") print(IMAGES[wrongAttempts]) print("\nGAME OVER") break wrongAttempts += 1 letters_guessed.append(letter) print("") hangman(choose_word())
print "" break else: print "Oops! That letter is not in my word: " + get_guessed_word( secret_word, letters_guessed) if remaining_lives == 0: print "no more lives :(" print "Sorry you lost the game :(" break print IMAGES[images_index[total_lives - remaining_lives]] print "you have remaining lives" + " - ", remaining_lives print "" letters_guessed.append(letter) remaining_lives -= 1 else: print "invalid input" # Load the list of words into the variable wordlist # So that it can be accessed from anywhere in the program word_list = words.choose_word() hangman(word_list)
import string from words import choose_word from image import IMAGES print(choose_word()) def is_word_guessed(secret_word, letters_guessed): ''' secret_word: ek string word jo ki user ko guess karna hai letters_guessed: ek list hai, jisme wo letters hai jo ki user nai abhi tak guess kare hai returns: return True kare agar saare letters jo ki user ne guess kiye hai wo secret_word mai hai, warna no False otherwise ''' # Iss function ko test karne ke liye aap get_guessed_word("kindness", [k, n, d]) call kar sakte hai def get_guessed_word(secret_word, letters_guessed): ''' secret_word: ek string word jo ki user ko guess kar raha hai letters_guessed: ek list hai, jisme wo letters hai jo ki user nai abhi tak guess kare hai returns: ek string return karni hai jisme wo letters ho jo sahi guess huye ho and baki jagah underscore ho. eg agar secret_word = "kindness", letters_guessed = [k,n, s] to hum return karenge "k_n_n_ss" ''' index = 0 guessed_word = "" while (index < len(secret_word)): if secret_word[index] in letters_guessed: guessed_word += secret_word[index]