Will Watts Senior Administrator, Security Analyst Staff member Joined Mar 4, 2012 Posts 6,594 Location %tmp% May 19, 2012 #1 So I was doing some exercises to help me learn python - I had to give it a break during exams, but I'm learning again. I can't get this bit of code to work, there is a logical error in it somewhere which I can't spot. The idea is, the user types a number and then the computer tries to guess it. It gets 20 attempts, if it goes through all 20, then the user wins and the game ends - if it guesses the correct number, it's SUPPOSED to break the while loop and tell the user that the computer won. I can't get it to work, all that happens is it goes through all 20 attempts (I've tried 100 attempts for testing to make sure it hits the number without having to run it 10 times) - after it's reached attempt 20 it tells the user that they won - even if it had guessed the number. Code: # The number guessing game # # The computer tries to guess your number within 20 tries. # Random import import random # Introduction print("\tThe Number Guessing Game") print("\t--Created by me") print("\n\nWelcome to the game. To play, pick a number between 1 and 100") number = input("What is your number? ") tries = 1 count = 1 while tries <=20 and count == 1: guess = random.randint(1, 100) print("\n", guess) if guess == number: print("\n The computer has guessed your number!") count += 1 tries += 1 if tries <=20: print("\nThe computer has won! It guessed, ", number) print("Thanks for playing!") elif tries >20: print("\nCongratulations! You have won! The computer couldn't guess your number.") input("\n Press Enter to exit the program") This always results in the while loop completing, even if the computer hits the right number. I've tried using a break, as below, but it still doesn't work in the above code. What is confusing me most, is that the code below works fine: Code: import random tries = 1 number = 42 while tries <=20: guess = random.randint(1, 100) print("\n", guess) if guess == number: print("\n The computer has guessed your number!") break tries += 1 input("") If the computer hits 42, the while loop breaks. Have I messed up the user input somehow? I've tried both the count, and break method to break the loop - now I'm stuck .