Python创建0-100随机加减法,输入#可退出,提出后计算成功率,#退出的题目不算概率里面
import random
import time
print("0-100随机加减法,输入#可退出")
i = 0
m = 0
start = time.time()
while True:
x = random.randint(0,100)
y = random.randint(0,100)
s = random.randint(0,1)
if s == 0:
answer = input("%d + %d = "%(x,y))
result = x + y
if s == 1:
if x<y:
x , y = y , x
answer = input("%d - %d = " % (x, y))
result = x - y
if answer == "#":
print("退出程序")
break
elif not answer:
print("未回答")
m += 1
elif answer.isdigit() and int(answer) == result:
print("这题答对了哦")
i += 1
m += 1
else:
print("答错了哦")
m += 1
finish = time.time()
l=i/m
print("一共答题%d,答对的题有%d,成功率%.2f%%" %(m,i,100*l))
print("耗时:%d"% int(finish-start))