Jaime Ojeda
Jaime Ojeda

Reputation: 19

How do I increase the score every time the user gets a question correct?

I'm making a very simple math quiz game and I want to increase the score every time the user gets a question correct. But when I run the code, the score stays at 1 and doesn't increase.

import random
for x in range (0,10):
   num_1 = random.randint(1,10)
   num_2 = random.randint(1,10)
   ansstring = "What is {0} + {1}:".format(num_1,num_2)
   answer = int(input(ansstring))
   score = 0
if answer == (num_1 + num_2):
   print("CORRECT!")
   score = score + 1
   print ("Score:",score)
else:
   print("INCORRECT! Correct answer is", (num_1 + num_2))
   
print ("Your score was", score,"/10")

Upvotes: 0

Views: 667

Answers (4)

DirtyBit
DirtyBit

Reputation: 16772

Put the variable score outside the loop, so it is not initialized back to 0 with each iteration.

Get the if-else conditions inside the for-loop to check for the current score every iteration:

import random
score = 0  # score var should be outside the loop
for x in range (0,10):
   num_1 = random.randint(1,10)
   num_2 = random.randint(1,10)
   ansstring = "What is {0} + {1}:".format(num_1,num_2)
   answer = int(input(ansstring))
   if answer == (num_1 + num_2):
       print("CORRECT!")
       score = score + 1
       print ("Score:", score)
   else:
      print("INCORRECT! Correct answer is", (num_1 + num_2))
   
print ("Your score was", score,"/10")

Upvotes: 0

Polypro
Polypro

Reputation: 87

Things you have to change:

  1. Score must be outside the for loop because you are giving it the value 0 each time.
  2. You must increment the score when the answer is right.
  3. Your if-else statements must be within the for loop

Here is how the code should look like:

import random

score = 0
for x in range (0,10):
   num_1 = random.randint(1,10)
   num_2 = random.randint(1,10)
   ansstring = "What is {0} + {1}:".format(num_1,num_2)
   answer = int(input(ansstring))
   if answer == (num_1 + num_2):
       print("CORRECT!")
       score = score + 1
       print ("Score:",score)

   else:
        print("INCORRECT! Correct answer is", (num_1 + num_2))

Upvotes: 0

Red
Red

Reputation: 27567

Simple, take score out of the for loop:

import random
score = 0
for x in range (0,10):
   num_1 = random.randint(1,10)
   num_2 = random.randint(1,10)
   ansstring = "What is {0} + {1}:".format(num_1,num_2)
   answer = int(input(ansstring))

   if answer == (num_1 + num_2):
      print("CORRECT!")
      score += 1
      print ("Score:",score)
   else:
      print("INCORRECT! Correct answer is", (num_1 + num_2))
       
print ("Your score was", score,"/10")

The reason yours didn't work is because, during each iteration, you are setting score = 0 over and over.

Upvotes: 0

10 Rep
10 Rep

Reputation: 2270

You need to put all the game play inside the for loop, and put the variable score outside of the for loop.

The reason why would be because everytime the for loop iterates, you are resetting the variable.

For the same reason, you need to check each sum, so you would need to put the if statements inside the for loop.

Code:

import random
score = 0
for x in range (0,10):
    num_1 = random.randint(1,10)
    num_2 = random.randint(1,10)
    ansstring = "What is {0} + {1}:".format(num_1,num_2) + " "
    answer = int(input(ansstring))
    if answer == (num_1 + num_2):
        print("CORRECT!")
        score = score + 1
        print ("Score:",score)
    else:
        print("INCORRECT! Correct answer is", (num_1 + num_2))
 
print ("Your score was", score,"/10")

Hope this helps!

Upvotes: 1

Related Questions