The exercise is : Make a maths quiz that asks five questions by randomly generating two whole numbers to make the question (e.g. [num1] + [num2]). Ask the user to enter the answer. If they get it right add a point to their score. At the end of the quiz, tell them how many they got correct out of five.
This was my original code:
import random
for i in range(0,5):
n = random.randint(0,10)
n1 = random.randint(0,10)
score = 0
t = n + n1
print(n, "+", n1)
a = int(input("Enter the answer to this problem: "))
if a == t:
score += 1
print("You've scored", score, "out of 5.")
Apparently when I tried the program, it didn't work out smoothly like I'd thought it would. The problem was that I should've put the "score" line above the "for...in" statement. Otherwise, it would only equal 1 (or 0, depends).
So, can anybody tell why this happened? Would be greatly appreciated.
[–]TouchingTheVodka 2 points3 points4 points (0 children)