结对编程——四则运算(python)

实验要求
–两个运算符,100 以内的数字,不需要写答案。

–需要检查答案是否正确,并且保证答案在 0..100 之间

–尽可能地多设置一些条件

也可以让同学们自选一个小应用程序进行结对编程的开发
请两位同学以结对编码(一个同学coding,另一个同学在旁边审核代码,之后再交换角色)的方式完成本次实验,并把程序、运算结果、博客体会等截屏发到其中一位同学的博客上,并在博客中体现另一位同学的学号(仅学号不体现姓名)
代码设计
该程序的主要设计分为3部分:算式生成、算式计算、逻辑主体

算式生成部分:定义一个随机生成运算符的函数randOperator,随机数生成函数根据randOperator返回的运算符随机生成一个非零整数(若运算符为÷)或任意整数(其他运算符)。

def randOperator():
    a = ['+','-','×','÷']
	#return a[random.randint(0,3)]
	return random.choice(a)
def randNozeros(operator):
	if operator == '÷':
		return random.randint(1,100)
	else:
		return random.randint(0,100)

randOperation(): 生成一道随机算术题目,包含两个运算符、三个操作数以及对应的答案。题目形式为num1 operator1 num2 operator2 num3 = ?。

def randOperation():
	operator1 = randOperator()
	operator2 = randOperator()
	num1 = random.randint(0,100)
	num2 = randNozeros(operator1)
	num3 = randNozeros(operator2)
	answer = computeResult(num1,num2,num3,operator1,operator2)
	return [f'{num1} {operator1} {num2} {operator2} {num3} = ?',answer]

算式计算部分:

computeResult计算由两个两步运算组成的算术表达式的结果。根据operator1与operator2的优先级分为两个分支:operator1优先级低,先进行operator2,else 先进行operator1。

def computeResult(num1,num2,num3,operator1,operator2):
        if (operator1 == '+' or operator1 == '-')and(operator2 == '×' or operator2 == '÷'):
                result = twoCompute(num1,twoCompute(num2,num3,operator2),operator1)
        else:
                result = twoCompute(twoCompute(num1,num2,operator1),num3,operator2)
        return result

由于randOperator生成的是字符,不能直接计算,因此需要通过twoCompute对字符进行判断,根据运算符定义运算逻辑。

def twoCompute(num1,num2,operator):
        if operator == '+':
                result = num1 + num2
        elif operator == '-':
                result = num1 - num2
        elif operator == '×':
                result = num1 * num2
        else:
                result = int(num1 / num2)
        return result

逻辑主体:
用户输入需要生成的题目数目。
循环生成指定数量的题目,将题目文本添加到subject列表,答案添加到answer列表,并打印出每个题目。
用户输入所有答案(以空格分隔),将其转换成整数列表inputAnswer。
比较用户答案与实际答案,统计答对的题目数rightNum。
计算并输出用户答题的正确率(百分比形式)以及用户输入的答案与正确答案列表。

num = eval(input("请输入需要生成的数目:\n"))
subject = []
answer = []
rightNum = 0
for i in range(0,num):
        t = randOperation()
        subject.append(t[0])
        answer.append(t[1])
        print(t[0])
print()
inputAnswe = input("题目生成完毕,请输入你的答案(每题答案用空格分开,如有小数只保留整数部分):\n")
inputAnswer = [int(num) for num in inputAnswe.split()] 
for i in range(0,len(answer)):
        if answer[i] == inputAnswer[i]:
                rightNum += 1
        i += 1
result = (rightNum/len(answer))*100
print(f'计算结果:\n你的正确率为{result:.2f}%\n你输入的答案:{inputAnswer}\n正确答案:{answer}')

完整py代码

点击查看代码
import random
def randOperator():
	a = ['+','-','×','÷']
	#return a[random.randint(0,3)]
	return random.choice(a)
def randNozeros(operator):
	if operator == '÷':
		return random.randint(1,100)
	else:
		return random.randint(0,100)
def computeResult(num1,num2,num3,operator1,operator2):
        if (operator1 == '+' or operator1 == '-')and(operator2 == '×' or operator2 == '÷'):
                result = twoCompute(num1,twoCompute(num2,num3,operator2),operator1)
        else:
                result = twoCompute(twoCompute(num1,num2,operator1),num3,operator2)
        return result
def twoCompute(num1,num2,operator):
        if operator == '+':
                result = num1 + num2
        elif operator == '-':
                result = num1 - num2
        elif operator == '×':
                result = num1 * num2
        else:
                result = int(num1 / num2)
        return result
        
def randOperation():
	operator1 = randOperator()
	operator2 = randOperator()
	num1 = random.randint(0,100)
	num2 = randNozeros(operator1)
	num3 = randNozeros(operator2)
	answer = computeResult(num1,num2,num3,operator1,operator2)
	return [f'{num1} {operator1} {num2} {operator2} {num3} = ?',answer]

num = eval(input("请输入需要生成的数目:\n"))
subject = []
answer = []
rightNum = 0
for i in range(0,num):
        t = randOperation()
        subject.append(t[0])
        answer.append(t[1])
        print(t[0])
print()
inputAnswe = input("题目生成完毕,请输入你的答案(每题答案用空格分开,如有小数只保留整数部分):\n")
inputAnswer = [int(num) for num in inputAnswe.split()] 
for i in range(0,len(answer)):
        if answer[i] == inputAnswer[i]:
                rightNum += 1
        i += 1
result = (rightNum/len(answer))*100
print(f'计算结果:\n你的正确率为{result:.2f}%\n你输入的答案:{inputAnswer}\n正确答案:{answer}')

运行结果

博客体会
此次实验由同学2252632与本人完成。上述代码仍存在可改进的地方,如:若operator为‘-’,则num1需大于等于num2,使得该运算符的计算结果为非负数;operator为‘×’时,num1和num2取值应为0 ~ 10,使得该运算符计算结果区间在0 ~ 100。
结对编程实验难就难在双方对问题的解决思路可能存在不一致的地方,因此在coding前需充分沟通交流,尝试不同的解决方案,以求最终成果能稳定高效地解决问题。

posted @   lilixian  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示