四则运算
参考代码博客园链接(https://www.cnblogs.com/shi1/p/8670776.htm)
def createarithmetic(self):
list = []
f1 = function1.function1()
f2 = function2()
operator_no = random.randint(1,3)
if operator_no == 1:
list.append(f1.createNum())
list.append(f2.createOperator())
list.append(f1.createNum())
elif operator_no == 2:
start = random.randint(0,2)
end = 0
if start == 0:
end == 0
else:
end = start +1
for i in range(1,4):
if i == start:
list.append("(")
list.append(f1.createNum())
if i == end:
list.append(")")
list.append(f2.createOperator())
list.pop()
elif operator_no == 3:
start = random.randint(0, 3)
end = 0
if start == 0:
end == 0
else:
end = start + 1 + random.randint(0,1)
if end >= 4:
end=4
for i in range(1, 5):
if i == start:
list.append("(")
list.append(f1.createNum())
if i == end:
list.append(")")
list.append(f2.createOperator())
list.pop()
else:
list.append(f1.createNum())
list.append(f2.createOperator())
list.append(f1.createNum())
return list
代码运行结果


浙公网安备 33010602011771号