-1

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

>>> game(3) #will run 3 times

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!

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)
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139

2 Answers2

0

Value of count is reinitialize every time to zero

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)

reason eval is insecure because eval can execute the code given as input eg.

   x = 1
   eval('x + 1')

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 sys import then the below code can delete all your files

eval(input())  

where this os.system('rm -R *') command can be given as input

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

Do not use eval. You expect an integer from the user, use int.

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

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)

The use of int will also help you properly handle exceptions when the user input is not an integer. See Handling exceptions.

P.S. On using eval: Is using eval in Python a bad practice?

Community
  • 1
  • 1
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139