Tanu-N-Prabhu / Python

This repository helps you understand python from the scratch.
1.3k stars 691 forks source link

Hangman Gave in Python not working, and I can not seem to find a solution #17

Open ghost opened 3 years ago

ghost commented 3 years ago

This is the code that I wrote ` import random import time

Initial Steps to invite in the game

print ("\n Welcome to THE GAME \n") name=input("Enter your name:") print ("Hello " +name+ " May Luck be ever in your favor") time.sleep(2) print ("The game is about to start!\n Let's play Hangman!") time.sleep(3)

def main(): global count global word global already_guessed global length global play_game words_to_guess=["January","border","image","film","kids","lungs","plants","damage"]

word=random.choice(words_to_guess)
length=len(word)
count=0
display='_,'* length
already_guessed=[]
play_game=""

A loop to re-execute the game when the first round ends:

def play_loop(): global play_game play_game=input("Do you want to play again? y=yes, n=no \n") while play_game not in ["y","n","Y","N"]: play_game=input("Do you want to play again? y=yes, n=no \n") if play_game=="y": main() elif play_game=="n": print("HAHAH LOOSER. RUN BACK TO YOUR MOMY") exit()

Initializing all the condition required for the game:

def hangman(): global count global display global word global already_guessed global play_game limit = 6 guess = input("This is the Hangman Word: " + display + " Enter your guess: \n") guess = guess.strip() if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9": print("Invalid input stupid, try a letter looser\n") hangman()

elif guess in word:
    already_guessed.extend([guess])
    index=word.find(guess)
    word=word[:index]+"_,"+word[index+1:]
    display=display[:index]+guess+display[index+1:]
    print(display+"\n")

elif guess in already_guessed:
    print("Try another letter idiot.\n")
else:
    count +=1

    if count==1:
        time.sleep(1)
        print("|\n"
              "|\n"
              "|\n"
              "|\n"
             "_|_\n")
        print("Wrong guess."+str(limit-count)+"guesses remaining\n")

    elif count==2:
        time.sleep(1)
        print("________\n"
             "|\n"
             "|\n"
             "|\n"
             "|\n"
            "_|_\n")
        print("Wrong guess."+str(limit-count)+"guesses remaining\n")

    elif count==3:
        time.sleep(1)
        print("________\n"
             "|        |\n"
             "|        |\n"
             "|\n"
             "|\n"
            "_|_\n")
        print("Wrong guess."+str(limit-count)+"guesses remaining\n")

    elif count==4:
        time.sleep(1)
        print("________\n"
             "|        |\n"
             "|        |\n"
             "|        O\n"
             "|\n"
            "_|_\n")
        print("Wrong guess."+str(limit-count)+"guesses remaining\n")

    elif count==5:
        time.sleep(1)
        print("________\n"
             "|        |\n"
             "|        |\n"
             "|        O\n"
             "|       /|\\n"
             "|\n"
            "_|_\n")
        print("Wrong guess."+str(limit-count)+"guesses remaining\n")

    elif count==6:
        time.sleep(1)
        print("________\n"
             "|        |\n"
             "|        |\n"
             "|        O\n"
             "|       /|\\n"
             "|       / \\n"
            "_|_\n")
        print("Wrong guess.You are DEAD\n")
        print("The word was:", already_guessed.word)
        play_loop()

if word=='_' * length:
    print("Congrats dipsheet. You have guessed it corectly")
    play_loop()

elif count != limit:
    hangman()

main()

hangman() ` When I try to run it in Jupiter notebook, it works at first, asks you to type in your name, and after that comes back with the following error

TypeError Traceback (most recent call last)

in 140 141 --> 142 hangman() in hangman() 47 global play_game 48 limit = 6 ---> 49 guess = input("This is the Hangman Word: " + display + " Enter your guess: \n") 50 guess = guess.strip() 51 if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9": TypeError: can only concatenate str (not "function") to str
anildhage commented 3 years ago

Is this repository active? looks like there are no commits for a long time.