Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ghost opened this issue May 11, 2021 · 1 comment
Open

Comments

@ghost
Copy link

ghost commented May 11, 2021

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant