1

I am making this multiplication program in python 2.7.5 for my sister, and I don't know how to count the correct answers. Here is the code:

import easygui
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
 answer = easygui.enterbox("What is " + str(i) + ' times 8?')
 if int(answer) == i * 8:
 easygui.msgbox("That is correct!")
 else:
 easygui.msgbox("Wrong!")
asked Jan 1, 2014 at 19:30

1 Answer 1

4

Why not just add a variable to keep count for you?

import easygui
correct_answers = 0 # start with none correct
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
 answer = easygui.enterbox("What is " + str(i) + ' times 8?')
 if int(answer) == i * 8:
 easygui.msgbox("That is correct!")
 correct_answers += 1 # increment
 else:
 easygui.msgbox("Wrong!")

You could improve your program by making the base number a variable, too, and using Python's str.format() rather than addition:

base = 8
...
 answer = easygui.enterbox("What is {0} times {1}?".format(i, base))
 if int(answer) == i * base:
 ...
answered Jan 1, 2014 at 19:32
Sign up to request clarification or add additional context in comments.

1 Comment

The reason that I did not is because I just started python and did not know how.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.