# Help with Number Guessing Game

 Blake Bullwinkel · November 2, 2014 Started learning Python today and want to practice if statements with a number guessing game. The computer selected an integer from 1-10. Odd thing is, when I enter 10 as my first guess, the "guess a little higher" response comes up when the integer must be lower. Can anyone find my mistake? Thanks in advance.

## Replies

 Colonel Panic · November 2, 2014 This code is impossible to debug.  You have no indentation, therefore we cannot see how the interpreter is structuring the code.   White space is very important in python.  You should also use the code highlighting feature in your post. Blake Bullwinkel · November 3, 2014 Sorry about that - I've attached screenshots of the code to this reply. Thank you for your help. Blake Bullwinkel · November 3, 2014 Screenshots uploaded Doug Fresh · November 11, 2014 ok, here is the problem:What you did:``import randomimport sysimport timeenter = "Please press enter to continue..."enter2 = "Press enter to exit"print("Hello! Welcome to a number guessing game")time.sleep(2)input(enter)time.sleep(1)print("Guess a number between 1 and 10")time.sleep(2)computernum = random.randint(1,10)computernum = str(computernum)Guess1 = input("My first guess is: ")if Guess1 == computernum: time.sleep(1) print("WOW! YOU WIN!") time.sleep(1) input(enter2) sys.exit(0)elif computernum < Guess1: time.sleep(1) print("Sorry! Guess a little lower") time.sleep(1) print("2 GUESSES REMAINING") time.sleep(1) Guess2 = input("My second guess is...")elif computernum > Guess1: time.sleep(1) print("Sorry! Guess a little higher") time.sleep(1) print("2 GUESSES REMAINING") time.sleep(1) Guess2 = input("My second guess is...")``Then, my keyboard exploded. But I was able to find the problem. I'm guessing you got an error when you tried to compare Guess1 to computernum. So you used computernum = str(computernum), which removed the error. In fact, by entering a letter, you would still return ("Sorry, guess a little higher"). The problem here is that when comparing the input (a string) to computernum, the interpreter doesn't see these values as equal. trying running this:``anumber = 0anumber = str(anumber)print(anumber)if 0 == anumber: print("yay")else: print("boo")``Note the return is False, meaning 0 is not equal to anumber, which is set to 0. This has to do with the way the values are stored in memory I believe. Something you cannot avoid. So you'll have to learn to coax the user's input toward the type of value you are looking for. ``import randomimport sysimport timeenter = "Please press enter to continue..."enter2 = "Press enter to exit"print("Hello! Welcome to a number guessing game")time.sleep(2)input(enter)time.sleep(1)print("Guess a number between 1 and 10")time.sleep(2)## Note the change below, Leaving computernum alonecomputernum = random.randint(1,10)Guess1 = int(input("My first guess is: "))if Guess1 == computernum:    time.sleep(1)    print("WOW! YOU WIN!")    time.sleep(1)    input(enter2)    sys.exit(0)elif computernum < Guess1:    time.sleep(1)    print("Sorry! Guess a little lower")    time.sleep(1)    print("2 GUESSES REMAINING")    time.sleep(1)    Guess2 = int(input("My second guess is..."))elif computernum > Guess1:    time.sleep(1)    print("Sorry! Guess a little higher")    time.sleep(1)    print("2 GUESSES REMAINING")    time.sleep(1)    Guess2 = int(input("My second guess is..."))``It's picking the "guess a little higher, because you have an elif condition, and no other condition can be met, so it treats it as an else statement. In the future, use an if statement rather than an elif when you first run the program. That way you know if your conditions are being met at all. Keep in mind, there are always several ways to solve this problem. As you learn more programming tricks, you will look back at this a say, "I'm proud of it, but it ain't pretty".As a note, I'd like to point out that there is a code button you can click when making posts with code. This saves everyone heaps of time, and data. Blake Bullwinkel · November 13, 2014 Thank you very much for you help! I've learned some new things and made an improved version. Vaibhav Patel · November 14, 2014 ``import randomimport sysimport timeenter = "Please press enter to continue..."enter2 = "Press enter to exit"print("Hello! Welcome to a number guessing game")time.sleep(1)input(enter)time.sleep(1)print("Guess a number between 1 and 10")time.sleep(1)## Note the change below, Leaving computernum alonecomputernum = random.randint(1,10)Guess1 = int(input("My first guess is: "))if Guess1 == computernum:    time.sleep(1)    print("WOW! YOU WIN!")    time.sleep(1)    input(enter2)    sys.exit(0)elif computernum < Guess1:    time.sleep(1)    print("Sorry! Guess a little lower")    time.sleep(1)    print("2 GUESSES REMAINING")    time.sleep(1)    Guess2 = int(input("My second guess is..."))elif computernum > Guess1:    time.sleep(1)    print("Sorry! Guess a little higher")    time.sleep(1)    print("2 GUESSES REMAINING")    time.sleep(1)    Guess2 = int(input("My second guess is..."))if Guess2 == computernum:    time.sleep(1)    print("WOW! YOU WIN!")    time.sleep(1)    input(enter2)    sys.exit(0)elif computernum > Guess2:    time.sleep(1)    print("Sorry! Guess a little higher")    time.sleep(1)    print("1 GUESSES REMAINING")    time.sleep(1)    Guess2 = int(input("My Third guess is..."))elif computernum < Guess2:    time.sleep(1)    print("Sorry! Guess a little lower...")    time.sleep(1)    print("1 GUESSES REMAINING")    time.sleep(1)    Guess2 = int(input("My Third guess is..."))if Guess2 == computernum:    time.sleep(1)    print("WOW! YOU WIN!")    time.sleep(1)    input(enter2)    sys.exit(0)elif computernum > Guess2:    time.sleep(1)    print("Sorry! You lost...")    time.sleep(1)    input(enter2)    sys.exit(0)elif computernum < Guess2:    time.sleep(1)    print("Sorry! You lost...")    time.sleep(1)    input(enter2)    sys.exit(0)`` Vaibhav Patel · November 14, 2014 The above script worked for me.
• 1

## Python

130,992 followers