Reputation: 19
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
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
Reputation: 87
Things you have to change:
for loop
because you are giving it the value 0 each time.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
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
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