Help with Number Guessing Game

+1 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.

/images/forum/upload/2014-11-03/42531c7c3544a6ffd9f51bb48c92dfd7.png/images/forum/upload/2014-11-03/c766e7590f577d6a0638d7f88537714e.png

Post a Reply

Replies

Oldest  Newest  Rating
0 Vaibhav Patel · November 14, 2014
The above script worked for me.
+2 Vaibhav Patel · November 14, 2014

import random
import sys
import time

enter = "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 alone
computernum = 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)
+1 Blake Bullwinkel · November 13, 2014
Thank you very much for you help! I've learned some new things and made an improved version.
+1 Doug Fresh · November 11, 2014
ok, here is the problem:
What you did:
import random
import sys
import time

enter = "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 = 0
anumber = 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 random
import sys
import time

enter = "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 alone
computernum = 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.
0 Blake Bullwinkel · November 3, 2014
Sorry about that - I've attached screenshots of the code to this reply. Thank you for your help. 

/images/forum/upload/2014-11-03/1825e79bdc85c1663370b7159a6f4a0b.png/images/forum/upload/2014-11-03/1f25f9c67c9720614245a8c06e723073.png
0 Blake Bullwinkel · November 3, 2014
Screenshots uploaded
+1 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.  
  • 1

Python

106,941 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator