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>It depends on how much you have learnt in that class, but adding the following line should allow you to accept both <code>30</code> and <code>guess = 30</code> (or even <code>foo=bar=30</code>):</p>

<pre><code>...
while guess != answer:
    guess = guess.split('=')[-1]    # Add this line
    try:
        guess = float(guess)
        ...
</code></pre>

<p>It simply splits the input using <code>=</code> as a delimiter and only uses the last part (<code>[-1]</code>).</p>

python python-3.x