Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hangman code problem
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,665 Jun-05-2020, 11:27 AM
Last Post: t0rn
  Hangman metro17 4 3,020 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,363 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Python hangman help A1395 11 7,160 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 70,715 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Hangman Help. 2skywalkers 4 4,190 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 12,080 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Simple Hangman Game Issue andrew95 2 4,338 Apr-02-2018, 02:24 PM
Last Post: andrew95
  Designing Hangman game spacetimeguy 2 5,126 Feb-02-2018, 08:55 PM
Last Post: spacetimeguy
  TKinter Hangman Game Bumble 1 20,533 Jul-19-2017, 06:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020