我正在为我的妹妹在python 2.7.5中制作这个乘法程序,我不知道如何计算正确的答案。这是代码:
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!")
答案 0 :(得分:4)
为什么不加一个变量来为你计算?
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!")
您可以通过将基数设为变量来改进程序,并使用Python的str.format()
而不是添加:
base = 8
...
answer = easygui.enterbox("What is {0} times {1}?".format(i, base))
if int(answer) == i * base:
...