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
Steampunkery
3,8742 gold badges22 silver badges28 bronze badges
1 Answer 1
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
jonrsharpe
123k31 gold badges278 silver badges488 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Steampunkery
The reason that I did not is because I just started python and did not know how.
lang-py