Take the 2-minute tour ×

I'm pretty new to Python (just started teaching myself a week ago), so my debugging skills are weak right now. I tried to make a program that would ask a user-submitted number of randomly-generated multiplication questions, with factors between 0 and 12, like a multiplication table test.

import math
import random

#establish a number of questions
questions = int(input("\n How many questions do you want?     "))     

#introduce score
score = 1

for question in range(questions):
    x = random.randrange(0,13)
    y = random.randrange(0,13)

    #make the numbers strings, so they can be printed with strings
    abc = str(x)
    cba = str(y)
    print("What is " + abc + "*" + cba +"?")

    z = int(input("Answer here:   "))
    print z
    a = x*y

    #make the answer a string, so it can be printed if you get one wrong
    answer = str(a)

    if z > a or z < a:
        print ("wrong, the answer is " + answer)

        #this is the line that's being skipped
        score = score - 1/questions
        print "Correct!"
        print ("\n")

finalscore = score*100
finalestscore = str(finalscore)
print (finalestscore + "%")

The idea was that every time the user gets a question wrong, score (set to 1) goes down by 1/question,so when multiplied by 100 it gives a percentage of questions wrong. However, no matter the number of questions or the number gotten wrong, score remains 1, so finalestscore remains 100. Line 26 used to be: if math.abs(z)-math.abs(a) != 0: but 2.7.3 apparently doesn't acknowledge that math has an abs function.

Such a simple accumulator pattern doesn't seem like it would be an issue, even for an older version of Python. Help?

share|improve this question

2 Answers 2

Try score = score - 1.0/questions

The problem is that you're doing integer division, which truncates to the nearest integer, so 1/questions will always give 0.

share|improve this answer
Knew I was missing something! Thanks so much. –  user1615629 Aug 22 '12 at 3:55

The problem is that you are using integers for all of your calculations. In particular, when you calculate 1/questions, it truncates (rounds down) to an integer because both values in the calculation are integers.

To avoid this, you could instead use 1.0/questions to make the calculations use floating point numbers instead (and not truncate)

share|improve this answer

Your Answer


