James Malkin
James Malkin

Reputation: 149

Why does the else not work correctly?

I am making a simple hangman game for a class. The current program does every step of the drawings, even if the user gets a correct answer.

import turtle
import random
import sys

while True:
    list = ['report', 'beach', 'mayor', 'score', 'overeat', 'load', 'battery', 'social', 'honor', 'risk']

    turns = 10
    guesses = ''
    err = 0

    usedw = random.choice(list)

    wn = turtle.Screen()
    draw = turtle.Turtle()

    length = len(usedw)

    print("Welcome to hangman your word is", length, "letters long.")

    while turns > 0:
        guess = input("Choose a character: ")
        guesses += guess;
        lengthr = 0
        for char in usedw:
            if char in guesses:
                print (char,)
                lengthr += 1;
            else:
                err += 1;
                if err == 1:
                    draw.goto(0,0)
                    draw.down()
                    draw.goto(200,0)
                elif err == 2:
                    draw.up()
                    draw.goto(0,0)
                    draw.down()
                    draw.goto(0,200)
                elif err == 3:
                    draw.up()
                    draw.goto(0,200)
                    draw.down()
                    draw.goto(100,200)
                elif err == 4:
                    draw.up()
                    draw.goto(100,200)
                    draw.down()
                    draw.goto(100,150)
                elif err == 5:
                    draw.up()
                    draw.goto(100,100)
                    draw.down()
                    draw.circle(25)
                elif err == 6:
                    draw.up()
                    draw.goto(100,100)
                    draw.down()
                    draw.goto(100,50)
                elif err == 7:
                    draw.up()
                    draw.goto(75,90)
                    draw.down()
                    draw.goto(125,90)
                elif err == 8:
                    draw.up()
                    draw.goto(100,50)
                    draw.down()
                    draw.goto(75,35)
                elif err == 9:
                    draw.up()
                    draw.goto(100,50)
                    draw.down()
                    draw.goto(125,35)
                    print("You loose.")
                    break
            if lengthr == length:
                print ("You won!")
                break
        cont = input("Would you like to continue (y/n) ")
        if cont == 'y':
            print("Alright!")
        else:
            print("Thanks for playing!")
            break
            sys.exit()

Upvotes: 1

Views: 71

Answers (2)

user8701049
user8701049

Reputation:

import turtle
import random
import sys

while True:
    list = ['report', 'beach', 'mayor', 'score', 'overeat', 'load', 'battery', 'social', 'honor', 'risk']

    turns = 10
    guesses = set()
    err = 0

    usedw = random.choice(list)

    wn = turtle.Screen()
    draw = turtle.Turtle()

    length = len(usedw)
    p_length = 0
    c_length = 0

    print("Welcome to hangman your word is", length, "letters long.")

    while turns > 0:
        guess = input("Choose a character: ")
        # 'previous' guesses length
        p_length = len(guesses)
        # Adds unused guesses only
        guesses.add(guess)
        # 'current' guesses length
        c_length = len(guesses)
        # detect UNIQUE guess entry and guess in usedw
        if (p_length != c_length and guess in usedw):
            for char in usedw:
                if guess == char:
                    print (char,)
                    # decrement remaining chars to match
                    length -= 1
        else: # either a duplicate/unmatched guess
            err += 1
            if err == 1:
                draw.goto(0,0)
                draw.down()
                draw.goto(200,0)
            elif err == 2:
                draw.up()
                draw.goto(0,0)
                draw.down()
                draw.goto(0,200)
            elif err == 3:
                draw.up()
                draw.goto(0,200)
                draw.down()
                draw.goto(100,200)
            elif err == 4:
                draw.up()
                draw.goto(100,200)
                draw.down()
                draw.goto(100,150)
            elif err == 5:
                draw.up()
                draw.goto(100,100)
                draw.down()
                draw.circle(25)
            elif err == 6:
                draw.up()
                draw.goto(100,100)
                draw.down()
                draw.goto(100,50)
            elif err == 7:
                draw.up()
                draw.goto(75,90)
                draw.down()
                draw.goto(125,90)
            elif err == 8:
                draw.up()
                draw.goto(100,50)
                draw.down()
                draw.goto(75,35)
            elif err == 9:
                draw.up()
                draw.goto(100,50)
                draw.down()
                draw.goto(125,35)
                print("You loose.")
                break

        # detect completion
        if 0 == length:
            print ("You won!")
            break

    cont = input("Would you like to continue (y/n) ")
    if cont == 'y':
        print("Alright!")
    else:
        print("Thanks for playing!")
        break
        sys.exit()

Upvotes: 0

Prune
Prune

Reputation: 77827

THE PROBLEM is in your in/correct determination logic:

for char in usedw:
    if char in guesses:
        print (char,)
        lengthr += 1;
    else:
        err += 1;
        if err == 1:
            ...

The for iterates through the characters of the word. For instance, if the word is "overeat", you will execute this loop seven times for each guess. Even if the player makes the best guess, "e", you will charge him/her with wrong guesses for the letters "ovrat", and draw five boy parts.


REPAIR

First, you have to determine whether the most recently guessed letter is in the word anywhere:

if guess in usedw:
    print (char,)
    lengthr += 1;
else:
    err += 1;
    if err == 1:
    ...

There are other problems and improvements, but this should get you moving.

Upvotes: 2

Related Questions