四则运算作业

2023-01-31 00:01:09 作业 运算

代码:

# -*- coding: utf-8 -*-
import random
from fractions import Fraction
from envs.py3k.Lib.symbol import except_clause
print ("小学四则运算测试:(结果保留1位有效数字)")
ops = ['+', '-', '*', '/'] # 锟斤拷锟斤拷锟�
ans = "" # 锟矫伙拷锟截达拷
num = 1 # 锟斤拷锟�
rightnum = 0
t = 0
while(1):
try:
while(1):
a = input("是否答题(退出请输入q或Q,否则按Enter键)")
if a not in ['q','Q']:
add1 = random.randint(1, 100) # 锟斤拷1
add2 = random.randint(1, 100) # 锟斤拷2
op = random.randint(0, 3) # 锟斤拷锟斤拷锟斤拷锟斤拷
eq = str(add1) + ops[op] + str(add2)
val = round(eval(eq),1) # 锟斤拷式锟斤拷

print ("Q%d: %s=" %(num,eq)) # 锟斤拷锟斤拷锟斤拷锟�

ans = input("A%d: "%num) # 锟矫伙拷锟截达拷
if val == eval(ans): # 锟斤拷确
rightnum += 1
print ("right!")
else: # 锟斤拷锟斤拷
print ("error. the right answer is %f" % val)
num += 1 # 锟斤拷锟斤拷锟斤拷锟�
t +=1
else:
print("right:%d" %rightnum) # 锟剿筹拷循锟斤拷
print("all:%d" %num)
s=round(rightnum/num,3)
print("正确率%f" %s)
break
except:
print("输入错误")
if a in ['q','Q']:
break

 

相关文章