1

I am a brand new programmer, and I have been trying to learn Python (2.7). I found a few exercise online to attempt, and one involves the creation of a simple guessing game.

Try as i might, I cannot figure out what is wrong with my code. The while loop within it executes correctly if the number is guessed correctly the first time. Also, if a lower number is guessed on first try, the correct code block executes - but then all subsequent "guesses" yield the code block for the "higher" number, regardless of the inputs. I have printed out the variables throughout the code to try and see what is going on - but it has not helped. Any insight would be greatly appreciated. Thanks! Here is my code:

from random import randint

answer = randint(1, 100)
print answer
i = 1


def logic(guess, answer, i):
    guess = int(guess)
    answer = int(answer)
    while guess != answer:
        print "Top of Loop"
        print guess
        print answer
        i = i + 1
        if guess < answer:
            print "Too low. Try again:"
            guess = raw_input()
            print guess
            print answer
            print i

        elif guess > answer:
            print "Too high. Try again:"
            guess = raw_input()
            print guess
            print answer
            print i

        else:
            print "else statement"

    print "Congratulations! You got it in %r guesses." % i


print "Time to play a guessing game!"
print "Enter a number between 1 and 100:"
guess = raw_input()
guess = int(guess)

logic(guess, answer, i)

I'm sure it is something obvious, and I apoloogize in advance if I am just being stupid.

  • 1
    Change `guess = raw_input()` to `guess = int(raw_input())`. In python3 it would have been easier to spot this since comparing a string with an integer raises an error. – Bakuriu Jun 28 '13 at 10:44

4 Answers4

3

You've noticed that raw_input() returns a string (as I have noticed at the bottom of your code). But you forgot to change the input to an integer inside the while loop.

Because it is a string, it will always be greater than a number ("hi" > n), thus that is why "Too high. Try again:" is always being called.

So, just change guess = raw_input() to guess = int(raw_input())

TerryA
  • 58,805
  • 11
  • 114
  • 143
0

Try this:

guess = int(raw_input())

As raw_input.__doc__ describes, the return type is a string (and you want an int). This means you're comparing an int against a string, which results in the seemingly wrong result you're obtaining. See this answer for more info.

Community
  • 1
  • 1
mikołak
  • 9,605
  • 1
  • 48
  • 70
0

Ok, I found your problem. The problem is in this Code:

    if guess < answer:
        print "Too low. Try again:"
        guess = raw_input()
        print guess
        print answer
        print i

    elif guess > answer:
        print "Too high. Try again:"
        guess = raw_input()
        print guess
        print answer
        print i

In the code above you are getting your input as string, but you try to compare it with integer. All you need to do is to convert the input to integer, like this:

guess = raw_input()
guess = int(guess)

This should solve your problem :)

Aviv
  • 456
  • 1
  • 7
  • 16
-1

I updated the program. You came out of the while loop because after you get guesss as input inside elif group, you forget to convert that to int, so it throwed back error. Now, corrected but you can also optimise it.

import sys

from random import randint

answer =  randint(1, 100)
'''randint(1, 100)'''
print (answer)
i = 1


def logic(guess, answer, i):
    guess = int(guess)
    answer = int(answer)
    while guess != answer:
        print ("Top of Loop")
        print (guess)
        print (answer)
        i = i + 1

        if guess < answer:
            print ("Too low. Try again:")
            guess = int(input())
            print (guess)
            print (answer)
            print (i)

        elif guess > answer:
            print ("Too high. Try again:")
            guess = int(input())
            print (guess)
            print (answer)
            print (i)

        else:
            print ("else statement")

    print ("Congratulations! You got it in %r guesses." % i)


print ("Time to play a guessing game!")
print ("Enter a number between 1 and 100:")
guess = input()
guess = int(guess)

logic(guess, answer, i)
Santhosh
  • 1,771
  • 1
  • 15
  • 25
  • 1
    Downvote... Not only is this answer over 10 minutes late (there's THREE other answers already stating thee exact same thing), you've also converted the code to python3 which is certainly not what OP wants. Also, it's non-obvious what you've changed because you copypasted all of OPs code. – l4mpi Jun 28 '13 at 11:02