Hi I am created a word guessing game.
I have pretty much completed the game, but I cannot get it to do one thing.
That is showing the users previous guess along with the new guess.

For example, I want it to be like this

Guesses Correct
apple no
blue no
house yes

Instead I only get my game to print the last guess, which in this case is "house".

I am defined a function that prints the Guesses and where or not it is correct, and calling this function
everytime a guess is entered.

I have tried creating a empty variable and adding the guessed word everytime a word is entered and the same with correct, and then printing them.

However the problem is, the guesses do no match up with the correct.
The above example would look something like this:

Guesses Correct
apple
blue
house no
no
yes

How would I go about in fixing this problem?

Here is some of the code from the game that tries to print this:

def informuser():
    print "\nYour guesses          correct          incorrect"
    print guessedwords, " "*30 + corrects, " "*30 + incorrects
    
def main()
    while (guesses != 0):
        for i in range(len(codeword)):
            if codeword == codewordguess:
                correct = correct + 1
            if codeword != codewordguess:
                incorrect = incorrect + 1
        guessedwords = guessedwords + "\n" + codewordguess
        corrects = corrects + "\n" + str(correct) 
        incorrects = incorrects + "\n" + str(incorrect) 
        incorrect = 0
        correct = 0
        informuser()

Recommended Answers

All 4 Replies

I think you should use lists and tuples instead of just using strings, for example you could define a list of the previous guesses like this

previousguesses = []

Then you append triples to this list, in the game's main loop, like this

def main():
    while .....
        previousguesses.append((codewordguess, corrects, incorrects))

Then your list's content might look like this:

[('apple', 3, 2), ('blue', 1, 3), ('house', 4, 1)]

To print the previous guesses you define

def informuser():
    ...
    for g, c, i in previousguesses:
        print g + " "*30 + str(c) + " "*30 + str(i)

Thanks Gribouillis, you are a life saver!
I have added to your rep!

Thanks :)

You might also want to consider a dictionary, with the guess as the key, and the Yes/No response as the value. It's simplier to understand in this case IMHO. Also, in the following code snippet, you don't use "i" for anything. That is you perform the same exact compare for however many letters are in codeword. I think you want to use
if codewordguess in codeword:
or something similiar

for i in range(len(codeword)):
            print "comparing", i, codeword, codewordguess
            if codeword == codewordguess:
                correct = correct + 1
##          if codeword != codewordguess:     ##  redundant if statement
            else:
                incorrect = incorrect + 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.