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>I copied and pasted your code into Python 3.5, and....apart from needing to indent everything after the while statement it worked fine.</p>

<p>Are you inputting just the number: 30</p>

<p>...or "guess = 30"? Because that does cause a problem since it's not a number.
You only need to input the number. :)</p>

<p>If you want to accept "guess = 30" then:</p>

<pre><code>import random
import re ###&lt;-Add this

guess = input("What is your guess?")
answer = random.randint(0,100)

while guess != answer:
    try:
        guess = re.sub("[^0-9]", "", guess) ###&lt;- Add this
        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>These two lines will use Regular Expressions to strip the input of any non numeric characters before processing.</p>

python python-3.x