Repeating code 3 times, and making sure input is only a number in Python 2.7
<p>I am trying to find out how to make my code repeat its self 3 times at the end of the set of questions and also I'm trying to make sure my validation is correct so that the input to the questions is only a number.  </p>

<p><strong>This is my code...</strong></p>

<pre><code>import random

correct = 0
name = raw_input("Please enter your name: ")
print 'ok',name, 'I need you to answer these 10 math simple maths questions'
for counter in range(10):
num1 = float(random.randint(1, 10))
num2 = float(random.randint(1, 10))
Math = random.choice(["+", "-", "*","/"])
print("Please solve:", num1, Math, num2)
user = int(input(""))

if Math == "+":
    answer = num1 + num2
elif Math == "-":
    answer = num1 - num2
elif Math == "*":
    answer = num1 * num2

elif Math == "/":
    answer = num1 / num2

if answer!= int:
    print ("Please reenter a number")
if user == answer:
    print("Correct!")
    correct = correct + 1
else:
    print("Incorrect")

print(name, " You Got", correct, "Out Of 10")
</code></pre>

<p>Your <code>answer != int</code> should be <code>if not isinstance(answer, int)</code>
Furthermore, you will get <code>ValueError</code> each time user tries to insert characters.</p>

<p>Because of this, you should edit your code to look like this:</p>

<pre><code>from __future__ import division
import random

for i in xrange(3):
    correct = 0
    name = raw_input("Please enter your name: ")
    print 'ok',name, 'I need you to answer these 10 math simple maths questions'
    for counter in xrange(3):
        num1 = random.randint(1, 10)
        num2 = random.randint(1, 10)
        Math = random.choice(["+", "-", "*","/"])
        print("Please solve: {} {} {}".format(num1, Math, num2))
        while True:
            try:
                user = int(raw_input(""))
            except ValueError:
                print ("Please reenter a number")
                continue
            break

        if Math == "+":
            answer = num1 + num2
        elif Math == "-":
            answer = num1 - num2
        elif Math == "*":
            answer = num1 * num2

        elif Math == "/":
            answer = round(num1 / num2, 1)

        if user == answer:
            print("Correct!")
            correct = correct + 1
        else:
            print("Incorrect")

    print("{} You Got {} Out Of 10".format(name, correct))
</code></pre>

<p>As you can see there was a lot things that you needed to change.</p>

python python-2.7