Number guessing game, unable to take the next guess
<p>Here is the concept of my game, the computer randomly generates a number from 1-100 and the player has to guess that number. If the number they guess is higher or lower the computer tells them so.</p>

<p>I added some code to make sure that the guess that the user enters is a number, but for some reason, it only works for their first guess. </p>

<pre><code>import random

x = random.randint(1, 100)
guess = input("Guess the number")

while guess.isnumeric() == True:

    if x &gt; int(guess):
        print("Too low, guess again")
        guess = input("Guess the number")

    if x &lt; int(guess):
        print("Too high, guess again")
        guess = input("Guess the number")

    if x == int(guess):
        print ("That is correct!")
        break

if guess.isnumeric() == False:
        print("Please enter a valid number")
        guess = input("Guess the number")
</code></pre>

<p>I don't really know how to else to explain it. But for example, if I guess the number 20 as my first guess, it would output too high or too low depending on the randomly generated number, but after that, if I input a bunch of random letters it would give me an error that the guess could not be compared to the randomly generated number.</p>

<p>Your <code>if</code> statements are all independent:</p>

<pre><code>if x &gt; int(guess):
    print("Too low, guess again")
    guess = input("Guess the number")

if x &lt; int(guess):
    print("Too high, guess again")
    guess = input("Guess the number")

if x == int(guess):
    print ("That is correct!")
    break
</code></pre>

<p>The second and third <code>if</code> statements will <em>always</em> test <code>guess</code> again, even if the first <code>if</code> test matched. And if the first <code>if</code> test matched and you entered a non-numeric <code>guess</code> value, those two tests will fail as the <code>int()</code> call will throw a <code>ValueError</code> exception.</p>

<p>You could tell Python that the tests are interdependent by using <code>elif</code> and <code>else</code>; now Python will only execute the <em>first matching block</em>, and skip the others entirely:</p>

<pre><code>if x &gt; int(guess):
    print("Too low, guess again")
    guess = input("Guess the number")

elif x &lt; int(guess):
    print("Too high, guess again")
    guess = input("Guess the number")

else:
    print ("That is correct!")
    break
</code></pre>

<p>This means that execution continuous <em>after</em> the <code>else</code> block when either the <code>if</code> or <code>elif</code> tests matched.</p>

<p>Note that I used <code>else</code> at the end; if the number is neither too high nor too low, the number <em>must</em> be equal, there is no other option. There is no need to test for that explicitly.</p>

<p>You are now repeating yourself however. You are asking for a guess in 3 different places. You could ask <em>once</em> and let the loop take care of asking for a new value:</p>

<pre><code>while True:
    while True:
        guess = input("Guess the number:")
        if guess.isnumeric():
            break
        print("Not a valid number, try again!")

    guess = int(guess)

    if x &gt; guess:
        print("Too low, guess again")

    elif x &lt; guess:
        print("Too high, guess again")

    else:
        print ("That is correct!")
        break
</code></pre>

<p>That's a lot less repetition already; a separate <code>while</code> loop asks for a number until it is actually numeric, and <code>guess</code> is converted to <code>int()</code> just once.</p>

<p>You could remove that nested <code>while True:</code> and just use the outer one here, the result would be the same, provided you use the <code>continue</code> keyword to skip the rest of the loop when you don't have a numeric value:</p>

<pre><code>while True:
    guess = input("Guess the number:")
    if not guess.isnumeric():
        print("Not a valid number, try again!")
        continue  # skip to the top of the loop again, so ask again

    guess = int(guess)

    if x &gt; guess:
        print("Too low, guess again")

    elif x &lt; guess:
        print("Too high, guess again")

    else:
        print ("That is correct!")
        break
</code></pre>

python random