1

Trying to do a simple guessing game program in python, but I'm more comfortable in java. When the correct number is entered, it says it is too high and won't exit the while loop. Any suggestions?

import random
comp_num = random.randint(1,101)
print comp_num
players_guess = raw_input("Guess a number between 1 and 100: ")
while players_guess != comp_num:
    if players_guess > comp_num:
        print "Your guess is too high!"
    elif players_guess < comp_num:
        print "Your guess is too low!"
    players_guess = raw_input("Guess another number between 1 and 100: ")
print "CONGRATULATIONS! YOU GUESSED CORRECTLY!"
4
  • Use elif for the second if... Commented Oct 30, 2014 at 0:14
  • still only outputs too high... Commented Oct 30, 2014 at 0:18
  • 4
    it is not a good idea to keep updating your question to reflect the answers (even if the answers are correct). It makes the answers look irrelevant, and removes any chance of other learning from your errors. Commented Oct 30, 2014 at 0:25
  • Some IDEs like PyCharm come with a handy debugger. Saves time asking questions online. Commented Oct 30, 2014 at 0:25

5 Answers 5

8

I would guess it is because you are comparing string and int. Whatever is captured from raw_input is captured as a string and, in Python:

print "1" > 100    # Will print true

For it to work, convert:

players_guess = raw_input("Guess a number between 1 and 100: ")

to

players_guess = int(raw_input("Guess a number between 1 and 100: "))
Sign up to request clarification or add additional context in comments.

5 Comments

thanks, guys! but it still won't exit the while loop once I enter the right number..
Or perhaps you forgot to cast your second raw_input to an int... like your updated question indicates. You should not update your question with answers, since you're only confusing things.
@MaxRegitnig like maksimov says, you probably forgot to cast your second raw_input at the bottom of the loop.
or just make 100 a string
Welcome to the wonderful world of dynamic typing. You'll learn to love it as much as hate it.
5

You are comparing a string to an int. That's why you get odd results.

Try this:

players_guess = int(raw_input("Guess a number between 1 and 100: "))

Comments

0
import random
comp_num = random.randint(1,101)
print comp_num
players_guess = int(raw_input("Guess a number between 1 and 100: "))
while players_guess != comp_num:
    if players_guess > comp_num:
        print "Your guess is too high!"
    elif players_guess < comp_num:
        print "Your guess is too low!"
   players_guess = int(raw_input("Guess another number between 1 and 100: "))
print "CONGRATULATIONS! YOU GUESSED CORRECTLY!"

you need to force the input to int

3 Comments

you missed the int from the 2nd raw_input call.
yea it was a typing mistake, down voting is not a soln
for what it is worth - I didn't downvote, I just stated a reason why someone might have done so - it does seem a little harsh to me.
0

Try this code:

import random
comp_num = random.randint(1,101)
print comp_num
players_guess = int(raw_input("Guess a number between 1 and 100: "))
while players_guess != comp_num:
    if players_guess > comp_num:
        print "Your guess is too high!"
    elif players_guess < comp_num:
        print "Your guess is too low!"
    players_guess = int(raw_input("Guess another number between 1 and 100: "))
print "CONGRATULATIONS! YOU GUESSED CORRECTLY!"

1 Comment

You could improve your answer by adding a short explanation.
0

in your code change in print formation and use 'int' datatype in numerical argument. i will change the cond so try it once:

    import random
comp_num = random.randint(1,101)
print ('comp_num')
players_guess = int(raw_input("Guess a number between 1 and 100: "))
while players_guess != comp_num:
    if players_guess > comp_num:
        print('Your guess is too high!')
    elif players_guess < comp_num:
        print('Your guess is too low!')
    players_guess = int(raw_input("Guess another number between 1 and 100: "))
print('CONGRATULATIONS! YOU GUESSED CORRECTLY!')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.