1

I've been making a 4 number guessing game to learn python that meets three criteria:

  • replayable
  • tells player how many tries it took to guess correct answer
  • tells player how many numbers are correct to steer player to right answer

I thought I met the criteria but there is this really weird bug that happens in the game. If you try to guess the number using trial and error; the game breaks and doesn't detect that your answer is right. If the answer is '[1, 2, 3, 4]' and you try to get the answer by doing '[1, 1, 1, 1]' then '[1, 2, 2, 2,]' and eventually get '[1, 2, 3, 4]'; the program will say that the 4 numbers match but it won't let you win the game and just asks you to play again. This bug has been really killing me and I hope the person reading understands what I'm trying to say.

Sorry about the big long block of code but, the problem could be anywhere here but I honestly cannot see it; I will annotate as best as I can to make it look less confusing. I just... why is this happening!?

def compareLists(a, b): # to compare the guessed numbers and random numbers
    return list(set(a) & set(b))
rNums = random.sample(range(10), 4) # random list of numbers
def start():
    count = 0 # count for number of tries
    global rNums
    gNums = [] # guessed numbers
    print(rNums) # cheating to save time
    flag = True # to stop the guessing loop

    while flag:
        print("Get ready to guess 4 numbers!")
        for i in range(0, 4): # asks the player 4 times to input a number
            x = int(input("Guess: "))
            gNums.append(x) # puts numbers in guessed numbers

        comparison = len(compareLists(rNums, gNums)) # storing the length of the list of similar numbers
        isCorrect = gNums == rNums # to check if lists are the same
        print("You guessed: ", gNums) # telling player what they have guessed

        if isCorrect: # if the lists are the same

            if count > 1:
                print("You win!! It only took you %d tries!" %count) # telling the player how many tries it took
            else: #congratulating the player on a flawless guess
                print("I CAN'T BELIEVE WHAT I'M SEEING!!!")
                print("YOU GOT IT IN ONE GO!!")
            count += 1 # increment count
            rNums = random.sample(range(10), 4) # generate new numbers
            gNums.clear()
            pAgain = input("Play again?")
            if pAgain.lower() in ('y', 'yes'): # replaying the game
                continue
            elif pAgain.lower() in ('n', 'no'):
                flag = False
            else:
                print("Incorrect syntax!")

        else:
            print("You guessed " + str(comparison) + " numbers right, keep guessing!") # tells the player how many numbers are similar so the player can get a more educated guess
            gNums.clear() # empties guessed numbers
            count += 1 # increment count
            print("Number of tries so far: %d" %count) # showing player number of tries so far
user3124306
  • 685
  • 1
  • 9
  • 20

1 Answers1

1

Your comparison for checking if the two lists are the same isn't working:

isCorrect = gNums == rNums # to check if lists are the same

The above code is checking if the two lists are identical, but the elements have to be in the same order.

For your test, you can just check if the number that match (ignoring order) is equal to the length of the list of numbers:

isCorrect = comparison == len(gNums) # to check if lists are the same

For more information on comparing lists regardless of order, see this answer.

Also, you should increment your count before you do your comparison with 1, or else your program will say you only took one go when you actually took two.

Community
  • 1
  • 1
samgak
  • 23,944
  • 4
  • 60
  • 82
  • Oh okay! But when I didn't repeat numbers in my guesses and have every number be different in the list; the program worked. Why does this only happen if the same number is repeated? – user3124306 Oct 17 '15 at 12:02
  • 1
    It doesn't only happen when the number is repeated though, the bug also happens when you answer all the numbers correctly, but in the wrong order. – samgak Oct 17 '15 at 12:45