Python Forum

Full Version: Hangman code problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey i got a few problems with my code, when i start it everything works but when i answer the right letter the program just stops. Im jusing python 3.7.




import random 
HANGMAN = (
"""
-----
|   |
|
|
|
|
|
|
|
--------
""",
"""
-----
|   |
|   0
|
|
|
|
|
|
--------
""",
"""
-----
|   |
|   0
|  -+-
|
|
|
|
|
--------
""",
"""
-----
|   |
|   0
| /-+-
|
|
|
|
|
--------
""",
"""
-----
|   |
|   0
| /-+-\
|
|
|
|
|
--------
""",
"""
-----
|   |
|   0
| /-+-\
|   | 
|
|
|
|
--------
""",
"""
-----
|   |
|   0
| /-+-\
|   | 
|  |
|
|
|
--------
""",
"""
-----
|   |
|   0
| /-+-\
|   | 
|  | | 
|  
|
|
--------
"""

)

print(HANGMAN[0])

play_again = True
while play_again:
    word_list = ["gurka", "sko", "python", "strumpa", "telefon", "dator", "hus", "samsung"]
    chosenword = random.choice(word_list).lower()
    guess = None 
    guessed_letters = [] #Lista över bokstäver som är gissade 
    blank_word = [] 
    for letter in chosenword:
        blank_word.append("_")
    attempts = 8


    while attempts > 0:


        if (attempts != 0 and "_" in blank_word):
            print(("\nDu har {} försök kvar.").format(attempts))
    
        try:
            guess = str(input("\nVälj en bokstav mellan A-Z")).lower()
        except:
            print("Bokstaven du valde är fel. Försök igen")
            continue
    
        else:
            if not guess.isalpha():
                print("Det är ingen bokstav. Försök igen.")
                continue
    
            elif len(guess)	> 1: 
                print("Det är mer än en bokstav. Försök igen")
                continue
    
            elif guess in guessed_letters:
                print("Du har redan gissat denna bokstav. Försök igen.")
                continue
            else:
                pass
    
        guessed_letters.append(guess)
    
        if guess not in chosenword:
            attempts-= 1
            print(HANGMAN[(len(HANGMAN)- 1) - attempts])  #Printar ut nästa bild
    
        else: #Är till för ifall det är två av samma bokstav i ett ord. 
            searchMore = True
            startsearchindex = 0
            while searchMore:
                try:
                    foundAtIndex = chosenword.index(guess, startsearchindex) 
                    blank_word[foundAtIndex] = guess
                    startsearchindex = foundAtIndex + 1
                except:
                    serachMore = False
    
        print("".join(blank_word))
    
        if attempts == 0:
            print("Game over! ordet var" + chosenword)
            print("\nVill du spela igen?")
            response = input("> ").lower()
            if response not in ("ja","yes"):
                play_again = False
                print("Tack för att du spelade")
            break
    
        if "_" not in blank_word:
            print(("\nGrattis! {} var ordet ").format(chosenword))
            print("\nVill du spela igen?")
            response = input("> ").lower()
            if response not in ("ja","yes","y"):
                play_again = False
                print("Tack för att du spelade")
            break
You misspelled searchMore on line 159, so your loop is constantly triggering an error and then failing to break out. Note that you could just get rid of searchMore, turn it into a while True: loop, and put a break in the except clause. And the except clause should include the appropriate error (ValueError), so that other errors can propagate normally. Finally, input returns a string, so you don't need to convert it to one on line 123.