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>Value of <code>count</code> is reinitialize every time to zero </p>

<pre><code>def game(n):
    count=0  # declare count here
    for _ in range(n): # you can use some variable here instead of _ to increase code clarity
        a=int(input('Enter a number:')) # As suggested use int instead of eval read end of post
        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(count)
</code></pre>

<p>reason eval is insecure because <code>eval</code> can execute the code given as input eg.</p>

<pre><code>   x = 1
   eval('x + 1')
</code></pre>

<p>user can give input like this which will result in 2 even more dangerous,the user can also give commands as input which can harm your system, if you have <code>sys import</code> then the below code can delete all your files</p>

<pre><code>eval(input())  
</code></pre>

<p>where this <code>os.system('rm -R *')</code> command can be given as input </p>

python