第八周-结对编程

作业题目

程序源代码

点击查看代码
import random
import time
import os
import json

def question():
    a = random.randint(0,100)
    b = random.choice(["+","-","*","/"])
    c = random.randint(0,100)
    d = random.choice(["+","-","*","/"])
    e = random.randint(0,100)
    while b == '/' and c==0:
        c = random.randint(0,100)
    while d == '/' and e==0:
        e = random.randint(0,100)
    question = f"{a}{b}{c}{d}{e}"
    return question

def create_que():
    q_list  = []
    while len(q_list) < 300:
        que = question()
        ans = eval(que)
        if ans in range(0,1000):
            q_list.append(que)
    with open("question.txt",'w') as file:
        for q in q_list:
            file.writelines(f"{q}\n")
    print(q_list)

def read_que():
    with open("question.txt",'r') as file:
        q_list = list(filter(None,file.read().split("\n")))
        return q_list

def ans_main():
    try:
        with open("data.json",'r') as f:
            data = json.load(f)
    except:
        data={
        "position":0,
        "t_num":10,
        "last_time":0,
        }

    try:
        q_list = read_que()
    except:
        create_que()
        q_list=read_que()

    i=0
    jump = False
    add = False
    right=0

    print("答题开始")
    print("输入@结束本次答题")
    os.system("pause")

    for q in q_list:
        os.system("cls")

        if i < data["position"] and jump == False:
            i+=1
            continue
        
        jump = True
        if add == False:
            i+=data["t_num"]
            add = True

        start_time = time.time()

        p = data["position"]
        print(f"第{p+1}题:")
        print(q)
        print("输入答案:")
        user_ans = input()

        if user_ans == "@":
            break

        ans = eval(q)
        ans = eval("{:.2f}".format(ans))
        print(f"正确答案是:{ans}")
        if eval("{:.2f}".format(eval(user_ans))) == ans:
            right+=1
            print("回答正确!")
        else:
            print("回答错误!")
        os.system("pause")
        data["position"] +=1

        if i == data["position"]:
            break
    
    os.system("cls")
    end_time = time.time()
    print(f"本次答题得分:{right*10} 分")
    print(f"用时:{round(end_time-start_time+data['last_time'],2)}秒")

    if user_ans != "@":
        print("恭喜你完成本次答题")
        data["t_num"] = 10
        data["last_time"] = 0
    else:
        data["t_num"] = i-data["position"]
        data["last_time"] = end_time-start_time
    with open("data.json",'w') as f:
        json.dump(data,f)

ans_main()

实现功能
1.生成300道四则运算题目

  • 生成含有两个运算符,计算数100以内的算式300道
  • 确保答案在0~1000之间
  • 采用txt文件存储题目

2.答题

  • 设定每轮作答10题
  • 如果中途退出答题可以从上一次答题的位置继续答题
  • 统计得分
  • 统计答题时间
  • 采用json文件储存数据信息
点击查看代码
def ans_main():
    try:
        with open("data.json",'r') as f:
            data = json.load(f)
    except:
        data={
        "position":0,
        "t_num":10,
        "last_time":0,
        }

    try:
        q_list = read_que()
    except:
        create_que()
        q_list=read_que()

    i=0
    jump = False
    add = False
    right=0

    print("答题开始")
    print("输入@结束本次答题")
    os.system("pause")

    for q in q_list:
        os.system("cls")

        if i < data["position"] and jump == False:
            i+=1
            continue
        
        jump = True
        if add == False:
            i+=data["t_num"]
            add = True

        start_time = time.time()

        p = data["position"]
        print(f"第{p+1}题:")
        print(q)
        print("输入答案:")
        user_ans = input()

        if user_ans == "@":
            break

        ans = eval(q)
        ans = eval("{:.2f}".format(ans))
        print(f"正确答案是:{ans}")
        if eval("{:.2f}".format(eval(user_ans))) == ans:
            right+=1
            print("回答正确!")
        else:
            print("回答错误!")
        os.system("pause")
        data["position"] +=1

        if i == data["position"]:
            break
    
    os.system("cls")
    end_time = time.time()
    print(f"本次答题得分:{right*10} 分")
    print(f"用时:{round(end_time-start_time+data['last_time'],2)}秒")

    if user_ans != "@":
        print("恭喜你完成本次答题")
        data["t_num"] = 10
        data["last_time"] = 0
    else:
        data["t_num"] = i-data["position"]
        data["last_time"] = end_time-start_time
    with open("data.json",'w') as f:
        json.dump(data,f)

程序运行截图


输入"@"之后:


重新启动程序:


答完最后一题:


博客体会
本次和学号2230117同学的结对编程,我们完成了对这一个简单的出题程序的设计和实现。采取了本人较为熟悉,也更加方便的python语言来实现程序。对于合作搭档不熟悉语言的问题,我选择了对他进行简单的教学。我认为使用更加合适的工具能够避免一些繁琐的编码过程。python语言的学习,对于有编程基础的同学来说是非常容易的。例如,如果使用我们都学习过的c++,java语言,必然会涉及到繁琐的数据结构,加重我们二人程序设计的负担。使用python语言就没有这些问题。
在这次结对编程实验中,虽然对比我个人单人编程,编码效率下降,但是另一位同学也在我编码的过程中发现了许多错误之处,和我一起进行逻辑上的修正,这也是我个人无法在短时间内做到的。
总的来说,这次实验使我学到了很多。我相信没有任何一个程序员能够一步做到完美,写出没有bug的程序。结对编程意义就在于二人相互监督,相互指出错误,来降低代码的错误率。但是如果二人水平不一致,也可能导致效率下降的问题。

posted @ 2024-04-24 15:44  bkali  阅读(29)  评论(0)    收藏  举报