Guess Random Number Why i m not able to enter input - python
<p>Below is my code to generate random number between 0 - 9 and checking with user input whether it is higher lower or equal. When I run the code, it is not taking input and showing</p>

<blockquote>
  <p>error in 'guessNumber = int(input("Guess a Random number between 0-9")) File "", line 1                                          '</p>
</blockquote>

<p>Can somebody please tell me where I'm making mistake</p>

<pre><code>#Guess Random Number
#Generate a Random number between 0 to 9

 import random
 turn = 0

 def guessRandom():
    secretNumber = random.randint(0,9)
    guessNumber = int(input("Guess a Random number between 0-9"))
    while secretNumber != guessNumber:
       if(secretNumber &gt; guessNumber):
          input("You have Guessed the number higher than secretNumber. Guess Again!")
          turn = turn + 1
       elif (secretNumber &lt; guessNumber):
          input("You have guessed the number lower than secretNumber. Guess Again! ")
       turn = turn + 1
    if(secretNumber == guessNumber):
        print("you Have Guessed it Right!")

guessRandom() 
</code></pre>

<p>I think <code>guessRandom()</code> was meant to be outside of the method definition, in order to call the method. The <code>guessNumber</code> variable never changes since the inputs are not assigned to be <code>guessNumber</code>, thus it will continuously check the initial guess. Also, the less than / greater than signs seem to conflict with the intended message. Additionally, <code>turn</code> is outside of the scope of the method.</p>

<pre><code>#Generate a Random number between 0 to 9

import random


def guessRandom():
    secretNumber = random.randint(0, 9)
    guessNumber = int(input("Guess a Random number between 0-9: "))

    i = 0
    while secretNumber != guessNumber:
        if secretNumber &lt; guessNumber:
            print "You have guessed a number higher than secretNumber."
            i += 1
        elif secretNumber &gt; guessNumber:
            print "You have guessed a number lower than secretNumber."
            i += 1
        else:
            print("you Have Guessed it Right!")

        guessNumber = int(input("Guess Again! "))

    return i

turn = 0
turn += guessRandom()
</code></pre>

<p>EDIT: Assuming you're using <a href="http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x"><code>input</code> in Python3</a> (or using <code>raw_input</code> in older versions of Python), you may want to except for <code>ValueError</code> in case someone enters a string. For instance,</p>

<pre><code>#Generate a Random number between 0 to 9

import random


def guessRandom():
    secretNumber = random.randint(0, 9)
    guessNumber = input("Guess a Random number between 0-9: ")

    i = 0
    while True:
        try:
            guessNumber = int(guessNumber)
        except ValueError:
            pass
        else:
            if secretNumber &lt; guessNumber:
                print "You have guessed a number higher than secretNumber."
                i += 1
            elif secretNumber &gt; guessNumber:
                print "You have guessed a number lower than secretNumber."
                i += 1
            else:
                print("you Have Guessed it Right!")
                break

        guessNumber = input("Guess Again! ")
    return i

turn = 0
turn += guessRandom()
</code></pre>

<p>I changed the <code>while</code> loop condition to <code>True</code> and added a break because otherwise it would loop indefinitely (comparing an integer to a string input value).</p>

python