结对编程
题目如下:
- 小学老师要每周给同学出300道四则运算练习题。
- 这个程序有很多种实现方式:
C/C++
C#/VB.net/Java
Excel
Unix Shell
Emacs/Powershell/Vbscript
Perl
Python
- 一个或两个运算符(a+b或a+b+c),100以内的数字,不需要写答案。
- 需要检查答案是否正确,并且保证答案在0.100之间
- 尽可能地多设置一些条件
小组成员
2152333_KID
2152221_xhy
代码
-
编写语言:Python
-
实现功能:
1.实现基础四则运算
2.保证减法有非负解,保证除法有正整数解
3.实现无括号双符号四则运算
4.保证运算一定正确且有非负整数解
5.存储答案用来检查回答的对错
6.每次输出一道题来计算
7.统计对错次数
import random
#KID编写
sym = ['+', '-', '*', '/']
res = []
yn = [0, 0]
for i in range(0, 300):
ch1 = random.choice(sym)
ch2 = random.choice(sym)
cnt = random.randint(1, 3)
if (cnt == 1):
num1 = random.randint(2, 101)
if (ch1 == '+'):
num2 = random.randint(0, 100-num1+1)
print("%d+%d=" % (num1, num2))
res.append(num1+num2)
if (ch1 == '-'):
num2 = random.randint(0, num1)
print("%d-%d=" % (num1, num2))
res.append(num1-num2)
if (ch1 == '*'):
num1 %= 10
num2 = random.randint(0, 11)
print("%d*%d=" % (num1, num2))
res.append(num1*num2)
if (ch1 == '/'):
num1 %= 10
num2 = random.randint(0, 11)
print("%d/%d=" % (num1*num2, num2))
res.append(num1)
else:
#xhy编写
num1 = random.randint(2, 11)
num2 = random.randint(2, 9)
if (ch1 == '+' and ch1 == '+'):
num1 = random.randint(1, 50)
num2 = random.randint(1, 50)
num3 = random.randint(0, 100-num1-num2)
print("%d+%d+%d=" % (num1, num2, num3))
res.append(num1+num2+num3)
if (ch1 == '+' and ch1 == '-'):
num1 = random.randint(1, 50)
num2 = random.randint(1, 50)
num3 = random.randint(0, 100-(num1+num2))
print("%d+%d-%d=" % (num1, num2, num3))
res.append(num1+num2-num3)
if (ch1 == '+' and ch1 == '*'):
num2 = random.randint(1, 11)
num3 = random.randint(1, 11)
num1 = random.randint(0, (100-num2*num3))
print("%d+%d*%d=" % (num1, num2, num3))
res.append(num1+num2*num3)
if (ch1 == '+' and ch1 == '/'):
num2 = random.randint(1, 11)
num3 = random.randint(1, 11)
num1 = random.randint(0, 100-num2)
print("%d+%d/%d=" % (num1, num2*num3, num3))
res.append(num1+num2)
if (ch1 == '-' and ch1 == '-'):
num2 = random.randint(1, 50)
num3 = random.randint(1, 50)
num1 = random.randint(num2+num3, 101)
print("%d-%d-%d=" % (num1, num2, num3))
res.append(num1-num2-num3)
if (ch1 == '-' and ch1 == '+'):
num2 = random.randint(1, 50)
num1 = random.randint(num2, 100)
num3 = random.randint(0, 100-(num1-num2))
print("%d-%d+%d=" % (num1, num2, num3))
res.append(num1-num2+num3)
if (ch1 == '-' and ch1 == '*'):
num2 = random.randint(1, 10)
num3 = random.randint(1, 10)
num1 = random.randint(num2*num3, 100)
print("%d-%d*%d=" % (num1, num2, num3))
res.append(num1-num2*num3)
if (ch1 == '-' and ch1 == '/'):
num2 = random.randint(1, 11)
num3 = random.randint(1, 11)
num1 = random.randint(num2*num3, 100)
print("%d-%d/%d=" % (num1, num2*num3, num3))
res.append(num1-num2)
if (ch1 == '*' and ch1 == '-'):
num1 = random.randint(1, 11)
num2 = random.randint(1, 11)
num3 = random.randint(num1*num2, 100)
print("%d*%d-%d=" % (num1, num2, num3))
res.append(num1*num2-num3)
if (ch1 == '*' and ch1 == '+'):
num1 = random.randint(1, 11)
num2 = random.randint(1, 11)
num3 = random.randint(0, 100-num1*num2)
print("%d*%d+%d=" % (num1, num2, num3))
res.append(num1*num2+num3)
if (ch1 == '*' and ch1 == '*'):
num2 = random.randint(1, 5)
num3 = random.randint(1, 5)
num1 = random.randint(1, 5)
print("%d*%d*%d=" % (num1, num2, num3))
res.append(num1*num2*num3)
if (ch1 == '*' and ch1 == '/'):
num1 = random.randint(1, 11)
num2 = random.randint(1, 11)
num3 = random.randint(1, 11)
while (num1*num2 % num3 != 0):
num3 = random.randint(1, 11)
print("%d*%d/%d=" % (num1, num2, num3))
res.append(num1*num2/num3)
#KID编写
if (ch1 == '/' and ch1 == '+'):
num1 = random.randint(1, 11)
num2 = random.randint(1, 11)
num3 = random.randint(1, 100-num1)
print("%d/%d+%d=" % (num1*num2, num2, num3))
res.append(num1+num3)
if (ch1 == '/' and ch1 == '-'):
num1 = random.randint(1, 11)
num2 = random.randint(1, 11)
num3 = random.randint(0, num1)
print("%d/%d-%d=" % (num1*num2, num2, num3))
res.append(num1-num3)
if (ch1 == '/' and ch1 == '*'):
num2 = random.randint(1, 5)
num3 = random.randint(1, 5)
num1 = random.randint(1, 5)
print("%d/%d*%d=" % (num1*num2, num2, num3))
res.append(num1*num3)
if (ch1 == '/' and ch1 == '/'):
num2 = random.randint(1, 5)
num3 = random.randint(1, 5)
num1 = random.randint(1, 5)
print("%d/%d/%d=" % (num1*num2*num3, num2, num3))
res.append(num1)
ans = int(input())
if (ans == res[i]):
print("Success")
yn[1] += 1
else:
print("Error")
yn[0] += 1
print("做对%d道,做错%d道。" % (yn[1], yn[0]))
实验结果截图
博客体会
KID:通过编程实现四则运算程序,我感受到了合作的力量和团队协作的重要性。在这个过程中,我的搭档和我互相协作,一起分工合作,相互交流,解决了许多问题。
通过结对编程实现四则运算程序,我感受到了合作和团队协作的重要性,我相信,在未来的编程实践中,我会更加注重团队协作,发挥每个人的优势,一起创造更加出色的成果。
xhy:实现四则运算程序,我学会了更好地理解和阅读别人的代码,同时也能够更好地表达自己的想法和思路。
我深刻地体会到了团队协作的重要性和合作的力量。我们需要共同思考和解决各种问题,同时还需要互相交流和审视代码,确保程序的正确性和可靠性。
总的来说,结对编程是一种非常有益的学习和实践方式,在未来的编程实践中,我会更加注重和搭档的协作,发挥每个人的优势。
标签:练习题,结对,random,num3,num1,num2,randint,四则运算,ch1 From: https://www.cnblogs.com/kid1412l/p/17294059.html