0

I wrote this script but it always returns the same answer ("Your guess is too high"), no matter what the user inputs. Any insight would be helpful.

import random

number = random.randint(1, 10) 

guess = input("Guess a number between 1 and 10: ")

if type(guess == int):
    print(number) # this prints the randint to show the code isn't working

    while(number != 0):
        if(guess > number):
            print("Your guess is too high!")
            break

        elif(guess < number):
            print("That's too low.")
            break

        elif(guess == number):
            print("Thats's right!")
            break

    else:
        print("Please enter a number.")
Munner
  • 51
  • 1
  • 1
  • 8
  • [Unable to reproduce](http://ideone.com/cMCOHZ) – Conduit Jan 03 '16 at 22:02
  • 1
    So I deleted my answer because some people decided to downvote without explaining why. Your code worked fine on Python 2.7 - http://i.stack.imgur.com/RTsuo.png - though there are scopes of improvements, the code does work on Python 2. – masnun Jan 03 '16 at 22:10
  • The `while` loop is worthless; you don't change the value of `number` inside the loop, and you break out of it in every case anyway. – chepner Jan 03 '16 at 22:13

4 Answers4

3
if type(guess == int):

This isn't doing what you expect. It always returns True because it's the equivalent to bool(type(False)). First make sure to convert your input to an int

guess = int(input("Guess a number between 1 and 10: "))

and then remove this if statement:

if type(guess == int):

Your problem is that this code:

 if(guess > number)

is always comparing a string to an int, so once you correct that your code will be fixed.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • OP should be prepared for `int()` to throw an exception if they always convert the user input. – Turn Jan 03 '16 at 22:09
3

Your while loop is useless, the problem of testing the input as an int is better handled with a try/except.

All together the correct answer is in Python3 :

import random

number = random.randint(1, 10) 
found = False

while  not found:
    try:
        guess = int(input("Guess a number between 1 and 10: "))
        if guess > number:
            print("Your guess is too high!")
        elif guess < number:
            print("That's too low.")
        elif guess == number:
            print("Thats's right!")
            found = True    
    except ValueError:
        print("Please enter a number.")
Gerard Rozsavolgyi
  • 4,834
  • 4
  • 32
  • 39
  • 1
    Probably should not print the number ;) You could also just move the `guess = int(input("Guess a number between 1 and 10: "))` inside the try – Padraic Cunningham Jan 03 '16 at 22:29
  • 1
    @PadraicCunningham: You are perfectly right ... i copied print from question and input can be kept inside the try. Will edit to clean code – Gerard Rozsavolgyi Jan 03 '16 at 22:34
  • Awesome! This worked very well. Now the code allows the user to keep guessing until the number is found. I don't understand how True/False and try statements work, so I'll read up on them to learn why this script works. Any suggestions for best resources on these topics? – Munner Jan 04 '16 at 20:53
  • 1
    while here is just a loop where we stay while our boolean found is false. try/except checks if something unusual happens like not beeing able to convert the input to an int. You can read a tutorial like https://docs.python.org/3.5/tutorial/. – Gerard Rozsavolgyi Jan 04 '16 at 21:06
  • I understand now how the "found = False" and "while not found" boolean logic works. To figure it out, I changed the "found" var to True, changed the while loop to "while found", and then changed the "found" var before the exception handler to False, and the code still works perfectly. Same logic, just in reverse. Thanks again. – Munner Jan 05 '16 at 19:04
2

I have just copied and pasted your code and it seems to function mostly correctly. There are some issues with it though. First, it appears that this is written for python 2 based on the way you are using the input function. However this is bad practice as the input() function in python 2 includes an implicit call to the eval() function which could allow for arbitrary code to be run.

In python 2 the better practice would be to use guess = int(raw_input("Guess a number between 1 and 10: ")).

In python 3, raw_input() has been removed and input() replaces it. So in python 3 you would use guess = int(input("Guess a number between 1 and 10: ")).

Your final else block is also indented where it should not be, although if you revise your code to make use of the advice given above, your if...else block is no longer necessary.

G. Grasso
  • 111
  • 4
1

That's because input returns a string in Python 3. You need to call int() to make it an integer type:

guess = int(input("Guess a number between 1 and 10: "))

You're also using the type() function incorrectly. You probably want the function isinstance(): if isinstance(guess, int):

Also, in Python, we don't need parentheses like you've used. You can simply do if guess > number:

TerryA
  • 58,805
  • 11
  • 114
  • 143