本次作业要求参见:[https://edu.cnblogs.com/campus/nenu/2019fall/homework/7631]
结对伙伴:王可非
功能1. 四则运算
import random def generate(): ope = ['+', '-', '*', '/'] num1 = random.randint(1, 10) num2 = random.randint(1, 10) num3 = random.randint(1, 10) num4 = random.randint(1, 10) ope1 = random.choice(ope) ope2 = random.choice(ope) ope3 = random.choice(ope) exp = str(num1) + str(ope1) + str(num2) + str(ope2) + str(num3) + str(ope3) + str(num4) return exp ret = [] for i in range(20): ret.append(generate()) k = 0 for el in ret: print(el) print('?', end="") answer = float(input()) if answer == eval(el): k += 1 print('答对啦,你真是个天才!') else: print('再想想吧,答案似乎是%d喔!' % (eval(el))) print('你一共答对%d道题,共20道题。' % k)
功能2. 支持括号
def generate(self): ope = ['+', '-', '*', '/'] bra = ['(', '', ')'] num1 = ru(0, 1) num1 = f(num1).limit_denominator(10) num2 = ru(0, 1) num2 = f(num2).limit_denominator(10) num3 = random.randint(1, 10) num4 = random.randint(1, 10) ope1 = random.choice(ope) ope2 = random.choice(ope) ope3 = random.choice(ope) left1 = random.randint(0, 1) left2 = random.randint(0, 1) left3 = random.randint(0, 1) right1 = random.randint(1, 2) right2 = random.randint(1, 2) right3 = random.randint(1, 2) if left1 == 0: left2 = 1 left3 = 1 if right1 == 2: right2 = 1 right3 = 1 else: right2 = 2 right3 = 1 else: if left2 == 0: left3 = 1 right1 = 1 if right2 == 2: right3 = 1 else: right3 = 2 else: left3 = 0 right1 = 1 right2 = 1 right3 = 2 exp = bra[left1] + str(num1) + str(ope1) + bra[left2] + str(num2) + \ bra[right1] + str(ope2) + bra[left3] + str(num3) + bra[right2] + \ str(ope3) + str(num4) + bra[right3]
功能3,和功能4的功能部分有重合,重合部分请看功能4
功能3. 限定题目数量,"精美"打印输出,避免重复
功能4. 支持分数出题和运算
def f4_answer(self,eq): from fractions import Fraction as f answer = f(eval(eq)).limit_denominator(1000) answer = str(answer) return(answer) def unlimited(self): try: num = 0 count = 0 for i in range(20): exq = f4().generate() print(exq, "=") _ans_right = f4().f4_answer(eq = exq) _ans = input("?") if _ans == _ans_right: print("答对了,你真是个天才!") num = num + 1 else: print("你再想想吧,答案似乎是", _ans_right, "喔!") count = count + 1 print("你一共答对", num, "道题,一共20道题。") except: print("输入有误") return (0) def limited(self,x): integer = "请输入正整数。" try: x = int(x) if x >= 0: for i in range(x): exq = f4().generate() right = f4().f4_answer(eq = exq) exq = exq + "=" print(exq.ljust(40), right) else: print(integer) except: print(integer) return(0) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument("-c", "--cin") args = parser.parse_args() if args.cin == None: f4().unlimited() else: f4().limited(args.cin)
功能5 未来
要求1 参考《构建之法》第4章两人合作,结对编程上述功能,要求每人发布随笔1篇 (代码是共同完成的,博客是分别完成的)。 (1) 给出每个功能的重点、难点、编程收获。(2)给出结对编程的体会,以及 (3) 至少5项在编码、争论、复审等活动中花费时间较长,给你较大收获的事件。 (10分)
在写四则运算的时候,首先我们是用栈做的,但是刚做到功能一的时候,代码已经有146行,代码比较冗余,容易出错,所以后面改变代码的写法,通过导入模块,用模块自带的功能,直接进行运算,把结果写成分式的形式。在写代码的过程中,有争吵,实现功能的时候该怎么写,也有讨论,讨论怎么做才能出结果。在相互的讨论中得到结果,相互促进,相互进步。
要求2 给出照片1张,包括结对的2位同学、工作地点、计算机,可选项包括其他能表达结对编程工作经历的物品或场景。
• 版本控制
代码Git地址:https://e.coding.net/xushanshan/sizeyunsuan.git