HangMan game in PYTHON

 



What is hangman? Libraries

Hangman is a paper and pencil guessing game for two or more players. One player thinks of a word, phrase or sentence and the other(s) tries to guess it by suggesting letters within a certain number of guesses. wikipedia

Code:

import random as r
listOfWords1 = ["pace", "space", "cross", "edge", "nine", "tame", "spawn", "real", "world", "seed"]
listOfWords2 = ["computer", "physics", "science", "sapiens", "building"]
def main():
    guesses = ""
    level = input("Easy(e)/Medium(m)/Hard(h):\n").lower()
    print("----------")
    if level == "e":
        turns = 5
        word = r.choice(listOfWords1)
    elif level == "m":
        turns = 3
        word = r.choice(listOfWords1)
    elif level == "h":
        turns = 3
        word = r.choice(listOfWords2)
    else:
        print(f" {level} is not a valid command, please try 'e' for easy, 'm' for medium and 'h' for hard level respectively. ")
        main()
    while turns > 0:
        failed = 0
        for char in word:
            if char in guesses:
                print(char)
            else:
                print("-")
                failed += 1
        if failed == 0:
            print("you won")
            ask = input("Do you want to play again? (y/n):\n").lower()
            if ask == "y":
                main()
            else:
                break
        guess = input("::\n").lower()
        guesses += guess
        print("----------")
        if guess not in word:
            turns -= 1
            print(f"You have {turns} more guesses. ")
            if turns == 0:
                print("you lost!!")
                print(f"The word was '{word}'. ")
                ask = input("Do you want to play again?(y/n)\n").lower()
                if ask == "y":
                    main()
                else:
                    break
if __name__ == "__main__":
    main()


@projectsofcode on instagram

Comments

Popular posts from this blog

Projects to consider as a bigener in PYTHON

Tic-Tac-Toe (multiplayer) using pthon