四则运算生成器
相对于上一个版本,此版本支持浮点数运算,支持交互式操作,支持错误校验并计算成绩,略微遗憾的是不支持分数计算。
码市链接:https://git.coding.net/poisonous-ant/sizeyunsuan.git
源代码如下:
import random
def Subject_Creater():
i = 0
a = 0
b = 0
print "How many questions do you want to test ?"
n = input()
while i<n : # 随机生成的题目数量为用户输入的数
x = round(random.uniform(0,10),2)
y = round(random.uniform(0,10),2)
z = random.choice("+-*/")
if z == "+":
result = round(x+y,2)
print x, z, y, "="
elif z == "-":
result = round(x-y,2)
print x, z, y, "="
elif z == "*":
result = round(x*y,2)
print x, "×", y, "="
else :
if y == 0:
i -= 1
else:
result = round(x/y,2)
print x, "÷", y, "="
temp = input()
if result == temp: # 用于记录正确和错误答题数以便给出成绩
print "Ture"
a += 1.0
else:
print "False ! The truth answer is:",result
b += 1.0
i += 1
print "Your grade is :",round(a/(a+b)*100,2)
Subject_Creater()
运行截图:
托管到Git:
另外:推荐一个超级简单的Git教程:
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
个人水平不足,欢迎大家指教。