How to ask a string again if the wrong answer is entered?
<p>I'm trying to get a string to keep repeating if the answer is wrong. How would I go about doing this? The code I have is below which works but doesn't repeat the answer.</p>

<pre><code>print("Hello there, what is your name?")
MyName = input()
print ("Nice to meet you " + MyName)
print ("What is 2 + 2?")

answer = input()
if answer is '4':
    print("Great job!")
elif answer != '4':
    print ("Nope! please try again.")
while answer != '4':
    print ("What is 2 + 2?")
    break
</code></pre>

<p>By now you've got more answers than you can handle, but here are another couple of subtleties, with notes:</p>

<pre><code>while True:   # loop indefinitely until we hit a break...

    answer = input('What is 2 + 2 ? ')    # ...which allows this line of code to be written just once (the DRY principle of programming: Don't Repeat Yourself)

    try:
        answer = float(answer)    # let's convert to float rather than int so that both '4' and '4.0' are valid answers
    except:     # we hit this if the user entered some garbage that cannot be interpreted as a number...
        pass    # ...but don't worry: answer will remain as a string, so it will fail the test on the next line and be considered wrong like anything else

    if answer == 4.0:
        print("Correct!")
        break   # this is the only way out of the loop

    print("Wrong! Try again...")
</code></pre>

python