我对 Python 很陌生(一周前才开始自学),所以我的调试技能现在很弱。我试图制作一个程序,它会询问用户提交的随机生成的乘法问题数量,系数在 0 到 12 之间,就像乘法表测试一样。
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)
print("\n")
#this is the line that's being skipped
score = score - 1/questions
else:
print "Correct!"
print ("\n")
finalscore = score*100
finalestscore = str(finalscore)
print (finalestscore + "%")
这个想法是,每次用户答错问题时,分数(设置为 1)会下降 1/问题,因此当乘以 100 时,它会给出错误问题的百分比。但是,无论问题的数量或错误的数量,分数仍然是1,所以finalestscore仍然是100。第26行曾经是:如果math.abs(z)-math.abs(a)!= 0:但是2.7。 3 显然不承认数学具有 abs 函数。
这样一个简单的累加器模式似乎不会成为问题,即使对于旧版本的 Python 也是如此。帮助?