python..I am stuck with one of the functions for and while loop
<p>I need to create a game to be played n times and adds numbers from 0 to 10. First number is entered by the player, second is generated by the program. After that the player has to guess the answer. If it is correct the program prints'correct' and same for the opposite('incorrect').In the end of the game the program prints how many correct answers the player got out of n times.
 The game runs n times</p>

<pre><code>&gt;&gt;&gt; game(3) #will run 3 times
</code></pre>

<p>I got all of it working correct but then how do I get the last part which is the program counts the correct answers and get the message printed?
Thank you!</p>

<pre><code>import random
def game(n):
    for _ in range(n):
        a=eval(input('Enter a number:'))
        b=random.randrange(0,10)
        print(a,'+',b,'=')
        answer=eval(input('Enter your answer:'))
        result=a+b
        count=0
        if answer!=result:

            print('Incorrect')
        else:
            count=count+1
            print('Correct!')

        print(_)

    print('You got',count,'correct answers out of',n)
</code></pre>

<p>Do not use <code>eval</code>. You expect an integer from the user, use <code>int</code>. </p>

<p>Then move the <code>count</code> variable outside the loop to avoid recreating new <code>count</code> variables with every iteration and resetting the value to zero.</p>

<pre><code>def game(n):
    count = 0
    for _ in range(n):
        a = int(input('Enter a number:'))
        b = random.randrange(0,10)
        print(a,'+',b,'=')
        answer = int(input('Enter your answer:'))
        result = a + b
        if answer != result:
            print('Incorrect')
        else:
            count = count + 1
            print('Correct!')
        print(_)
    print('You got',count,'correct answers out of',n)
</code></pre>

<p>The use of <code>int</code> will also help you properly handle exceptions when the user input is not an integer. See <a href="https://docs.python.org/2/tutorial/errors.html" rel="nofollow">Handling exceptions</a>.</p>

<p>P.S. On using <code>eval</code>: <a href="http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice">Is using eval in Python a bad practice?</a></p>

python