20180925-6 四则运算试题生成

20180925-6  四则运算试题生成

作业要求参见【https://edu.cnblogs.com/campus/nenu/2018fall/homework/2148

 

一.结对伙伴:祝玮琦

二.代码地址:https://git.coding.net/sunsss/two.git

三.要求

要求1:

(1) 给出每个功能的重点、难点、编程收获

 

①功能1的重点是数字和符号都要随机生成,Python中要运用random标准库进行解决。

②功能2的重点是括号要随机生成,依旧要运用random标准库解决。其中的难点是括号生成的顺序怎么解决,运用while,if来解决。

③功能3的重点是如何使题目不出现重复的情况,用os标准库解决。其中的难点就如何支持小数。

④功能4的重点是支持分数,用os ,fractions的标准库解决,难点真的很多,因为输出的数的类型要为浮点型,生成的分子分母就要要有位数的限制,所以很困难。

 

(2) 给出结对编程的体会

 

结对编程是一个相互学习,相互进步的过程。理论上来说,好处有三点,第一结对编程可以提供质量较好的代码,了解别人的思路和想法,从而努力的提高自己的水平;第二结对编程工作效率更高;第三结对编程使得两个人都被迫提高了工作效率。如果我单独工作,在遇到困难的时候,并不是立刻积极地去解决问题,这时或许会上网和网友聊聊天,看看无关的网站等等。有可能因为这种情况的产生,大半天的时间都浪费了。是利大于弊的情况。但就我个人的情况来说,我与我的结对同学,由于我们两个处于一样的水平,初级的学习状态,使得我们两个要花费比单人编程更多的时间,在每一个问题上都有反复的争论,反复的测试,最后在用百度查找的方式来验证观点,方法的对错,整个结对的过程结束后,我们彼此都觉得身心俱疲(请老师不要问我,为什么不找其他的同学结对,首先为了符合老师的要求,结合自身的课余时间,我无法找到其他符合标准的人;其次,比起一个我不熟悉水平的人,不知道什么会什么不会的人,我们两个虽然水平都很次,但是一起生活了很久,已经不用在如脾气,性格,沟通上进行磨合,相比之下,一起编程更有优势)在这个过程中,我认为我们此次过程弊大于利。我会珍惜之后的结对编程,团体合作,用最快的速度进步和成长。

 

(3) 至少5项在编码、争论、复审等活动中花费时间较长,给你较大收获的事件

 

第一:由于学习python的时间还不够长,四则运算的难度对我来说很大,加上对编码本就不是很熟练,经过这次作业,感觉编码比上次作业要编的好那么一点点了。

第二:对于题目的理解,我们两个人持有不同的看法。这种分歧出现在功能一和功能二上,我认为这两个功能可以通过编译一个代码来实现,而我的同伴认为这两个功能应该通过两个代码来实现,我们因此参考了一下前几届同学的作业,发现这两种情况都存在,并没有定论。最后我和我的同伴,将两个功能通过两个代码来进行实现。

第三:在编码过程中,由于对题目理解的错误,在最初编码时,自己设定了20道题,并不是要求中的随机生成四则运算,但是结果却是相同的,还因此得意忘形了一会儿,之后发现问题,进行了改正,耽误了较多的时间。

 

如图所示:

第四:花费时间最多的就是编代码的过程,单人编程的时候许多事情不用在征求别人的意见,但是结对编程中函数的使用,怎么使用等一系列问题都要两个人商量,都要进行尝试证明哪种方法更好。

第五:在测试的运行的过程中,将为文件转化为exe的格式时,电脑一直弹出防火墙进行拦截,期间尝试了很多方法,最终将问题解决了。

要求2 :给出一张照片

要求3:用Git上传

四则运算:

先把代码分别附上:

功能1:

import random 
ops = ['+','-','*','/']
com = input('>') #用户输入
cot = 0 #答对的题
x = 0
while x < 20 :
    s1 = random.randint(1,10)
    s2 = random.randint(1,10)
    s3 = random.randint(1,10)
    s4 = random.randint(1,10)
    op1 = random.choice(ops) #随机运算符
    op2 = random.choice(ops)
    op3 = random.choice(ops)
    while op1 == op2 == op3:
        op1 = random.choice(ops) #随机运算符
        op2 = random.choice(ops)
        op3 = random.choice(ops)
    eq = (str(s1)+op1+str(s2)+op2+str(s3)+op3+str(s4))
    res = eval(eq) 
    if len(str(res) ) > 5:
        continue
    x += 1
    print(eq)
    in_res =eval( input('?'))
    if in_res == res:
        print('算对啦,你真是个天才!')
        cot += 1
    else:
        print('再想想吧,答案似乎是%s喔!'%res)

print('你一共答对%s道题,共20道题'%cot)

功能2:

from random import randint
from random import choice
ops = ['+','-','*','/']
bra = ['(', '', ')']
com = input('>') #用户输入
cot = 0 #答对的题
x = 0
while x < 20 :
    s1 = randint(1,10)
    s2 = randint(1,10)
    s3 = randint(1,10)
    s4 = randint(1,10)
    op1 = choice(ops) #随机运算符
    op2 = choice(ops)
    op3 = choice(ops)
    """括号"""
    bra_1 = ['' , '' ,'']
    bra_2 = ['' , '' , '']
    i = ii =0
    while (i ==0 and ii ==2) or abs(i-ii)==1 
        or ii < i  :
        i = randint(0,2)
        ii = randint(0,2)

    bra_1[i] = '(';   bra_2[ii]=')'

    while op1 == op2 == op3 :
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)

    eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
    bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
    + str(s4) + bra_2[2]
    res = eval(eq) 
    if len(str(res) ) > 5:
        continue
    x += 1
    print(eq)
    in_res =eval( input('?'))
    if in_res == res:
        print('算对啦,你真是个天才!')
        cot += 1
    else:
        print('再想想吧,答案似乎是%s喔!'%res)

print('你一共答对%s道题,共20道题'%cot)

功能3:

from random import randint
from random import choice
import os 
ops = ['+','-','*','/']
bra = ['(', '', ')']
com = input('>') #用户输入
com_list = com.split()
while com_list[2].isdigit() == False:
    print('题目数量必须是正整数')
    com = input('>') #用户输入
    com_list = com.split()

def xx():
    s1 = randint(1,10)
    s2 = randint(1,10)
    s3 = randint(1,10)
    s4 = randint(1,10)
    op1 = choice(ops) #随机运算符
    op2 = choice(ops)
    op3 = choice(ops)
    """括号"""
    bra_1 = ['' , '' ,'']
    bra_2 = ['' , '' , '']
    i = ii =0
    while (i ==0 and ii ==2) or abs(i-ii)==1 
        or ii < i  :
        i = randint(0,2)
        ii = randint(0,2)

    bra_1[i] = '(';   bra_2[ii]=')'

    while op1 == op2 == op3 :
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)

    eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
    bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
    + str(s4) + bra_2[2]
    res = eval(eq) 
    return [eq,res]



eq = [];  res = []
while len(res) < int(com_list[2]):
    a = xx()
    if a[1] in res or len((str(a[1])) ) >6: #结果一样的直接就不要
        continue
    eq.append(a[0])
    res.append(a[1])

f= open('题目.txt','w')
for i in range(len(eq)):
    print('{0:15}'.format(eq[i]),end = '')
    print(res[i])
    xxx = 17 - len(eq[i])
    f.write(str(eq[i]+' '*xxx))
    f.write(str(res[i])+'
')
f.close()
os.system('题目.txt')  #决定是否打开txt

功能4:

import os
from random import randint
from random import choice
from fractions import Fraction
ops = ['+','-','*','/']
bra = ['(', '', ')']
com = input('>') #用户输入
com_list = com.split()
while com_list[2].isdigit() == False:
    print('题目数量必须是正整数')
    com = input('>') #用户输入
    com_list = com.split()

def xx():
    s1 = randint(1,10)
    s2 = randint(1,10)
    s3 = randint(1,10)
    s4 = randint(1,10)
    op1 = choice(ops) #随机运算符
    op2 = choice(ops)
    op3 = choice(ops)
    """括号"""
    bra_1 = ['' , '' ,'']
    bra_2 = ['' , '' , '']
    i = ii =0
    while (i ==0 and ii ==2) or abs(i-ii)==1 
        or ii < i  :
        i = randint(0,2)
        ii = randint(0,2)

    bra_1[i] = '(';   bra_2[ii]=')'

    while op1 == op2 == op3 :
        op1 = choice(ops) #随机运算符
        op2 = choice(ops)
        op3 = choice(ops)

    eq = bra_1[0] + str(s1) + op1 + bra_1[1] + str(s2) + 
    bra_2[0] + op2 + bra_1[2] + str(s3) + bra_2[1] + op3 
    + str(s4) + bra_2[2]
    res = Fraction(eval(eq)) 
    return [eq,res]

运行截图:

功能1:

功能2:

功能3:

 

 功能4:

结对编程环境:

软件:pycharm

地点:寝室

原文地址:https://www.cnblogs.com/swn321/p/9733183.html