Number guessing game: How can I accept input that says "guess =" before the number?
<pre><code>import random
guess = input("What is your guess?")
answer = random.randint(0,100)

while guess != answer:
    try:
        guess = float(guess)
        if guess &gt; answer:
            print ("Your guess is too high!")
        elif guess &lt; answer:
            print ("Your guess is too low!")
        elif guess == answer:
            print ("Congratulations!")
            break
        guess = input("What is your guess?")
        continue
    except ValueError:
        print ("Bad input. Try again!")
        guess = input("What is your guess?")
</code></pre>

<p>So my code works except that when i enter for example: guess = 30, it seems the input as invalid...how can I make it so it accepts it as a correct guess?</p>

<p>New to python here :)
Thanks.</p>

<p>So I reordered your code:</p>

<ul>
<li><p>I narrowed <code>try/except</code> down to just the line which can cause the ValueError, because that makes it clear what particular error this <code>except</code> block is trying to handle - puts the error handling near the source of the error. This also makes a good reason to have <code>continue</code> because it now skips the "too high/too low" code, instead of in your code it does nothing.</p></li>
<li><p>I changed the <code>while</code> loop condition so it says <code>while True:</code> to make it an infinite loop. In your code, <code>while guess != answer</code> implies that it will break when the guess is correct, you actually use <code>break</code> to exit the loop, so that is misleading. It would be sensible to use <code>while guess != answer</code> and not have <code>break</code> anywhere, but if you have to use <code>break</code> as part of the assignment then my code has some 'reason' to use it (breaking an infinite loop).</p></li>
<li><p>I moved the <code>guess = input(...)</code> code inside the loop only, because it doesn't need to be duplicated at the top or inside the error handling. (Imagine if you had to change the text, 3 places is more annoying than 1 place).</p></li>
<li><p>There are lots of ways to handle the <code>guess = 30</code> input. You could literally handle that, and only that, by looking for <code>if "guess = " in guess:</code> and then if that text matches, use <code>guess = guess.replace("guess = ", "")</code> to replace it away, leaving just the number. Or use regular expressions, as another answer has used to drop text and leave digits, or use string <code>split()</code>, as yet another answer has. My answer here has <code>filter()</code> which filters something using a test - in this case it tests if something is a number, and only allows numbers through, so it drops all the text. Same as the regex is doing, really, just a different approach.</p></li>
</ul>

<p>code:</p>

<pre><code>import random

answer = random.randint(0,100)

while True:
    guess = input("What is your guess?")

    # filters out only the numbers
    # and makes them into a string, e.g.
    # 1) "guess = 30"
    # 2) [3,0]
    # 3) "30"
    guess = ''.join(filter(str.isdigit, guess))

    try:
        guess = float(guess)
    except ValueError:
        print ("Bad input. Try again!")
        continue

    if guess &gt; answer:
        print ("Your guess is too high!")
    elif guess &lt; answer:
        print ("Your guess is too low!")
    else:
        print ("Congratulations!")
        break
</code></pre>

<p><a href="https://repl.it/D80v/1" rel="nofollow">Try it online at repl.it</a></p>

python python-3.x