Myapp.exe -n 10

1.需要生成随机数
2.循环结构生成相应数目的题目
3.判断题目的对错，并统计正确率
4.使用python2.7实现

# 程序

# coding=gbk
import random
import fractions
import string
i=0
result=0
temp=0
print"请输入想要生成的题目数量："
n=input()
while i<n:

first_num=random.randint(1,10)
second_num=random.randint(1,10)
third_num=random.randint(1,10)
fourth_num=random.randint(1,10)
a = random.choice("+-*/")
if second_num==1:
x=first_num
else:
x=fractions.Fraction(first_num,second_num)
if fourth_num==1:
y=second_num
else:
assert isinstance(fourth_num, object)
y=fractions.Fraction(second_num,fourth_num)
if a=="+":
result=x+y
faker=str(result)

print  x,a,y,"="
elif a=="-":
result=x-y
faker=str(result) #生成字符型
print x,a,y,"="
elif a=="*":
result=x*y
faker=str(result)
print x,"x",y,"="
elif a=="/":
result=x/y
faker=str(result)
print x,"÷",y,"="

if faker ==answer:         # 正确
temp=temp+1
print "right！"
else:                         # 错误
print 'error. the right answer is %s' % result
i += 1
print  "您答对%d题" % temp


# 代码地址（coding）

https://coding.net/u/run-in-way/p/homework/git/blob/master/sizeyunsuan.py

posted @ 2017-03-03 22:22  昊哥哥  阅读(600)  评论(4编辑  收藏  举报