You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python代码逻辑错误排查:在线测验排名程序输出异常

在线测验竞赛排名计算问题

问题描述

有n名候选人参与在线测验竞赛,题目、答案及评分规则如下:

Q.No. 1 2 3 4 5
Ans: A B B A C

每题2分,答错扣0.5分(即每题分值的25%),未答(选X)不扣分。需编写Python函数,接收候选人数量及答案选项,按总分计算排名。

输入输出格式

输入

候选人数量,各候选人的答案选项(每行一个候选人的5个答案,用空格分隔)

输出

第一行输出Rank Candidates Total,后续每行对应排名、候选人编号(如C1、C2)、总分,同分候选人并列排名,用逗号分隔编号。

现有代码

A=[]
G=[]
C=["A","B","B","A","C"]
Candidates=[]
n=int(input())
for i in range(0,n):
    A.append(list(map(str,input().split(" "))))
for i in range(0,n):
    G.append(0)
    Candidates.append("C"+str(i+1))
    for j in range(0,5):
        if A[i][j]==C[j]:
            G[i]+=2
        elif A[i][j]=="X":
            G[i]+=0
        else:
            G[i]-=0.5


#find the elements of the array and print the name and marks


class output:
    def __init__(self) -> None:
        pass
    def search(i):
        cand=[]
        x=max(G)
        ind =G.index(x)
        cand.append(Candidates[ind])
        G[ind]=0
        y=cand[0]
        while x==max(G):
            next_num = G.index(x)
            cand.append(Candidates[next_num])
            G[next_num]=0
        if len(cand)>2:
            y = ','.join(map(str, cand)) 
        print(i+1,y,x)
        return x
        
print("Rank Candidates Total")        
for i in range(0,n):
    x = output.search(i) 

测试用例与异常情况

测试用例1

输入:

5
A C A X B
A B C D X
X X X X C
A A A A A
B X X B A

程序输出:

Rank Candidates Total
1 C2 3.0
2 C4 2.5
3 C3 2
4 C1 0.5
5 C1 0

预期输出:

Rank Candidates Total
1 C2 3.0
2 C4 2.5
3 C3 2.0
4 C1 0.5
5 C5 -1.5

测试用例2

输入:

3
A D D D D
C C C C C
X X X X X

程序输出:

Rank Candidates Total
1 C1 0.0
2 C1 0
3 C1 0

预期输出:

Rank Candidates Total
1 C1,C2,C3 0.0

代码错误分析与修正

存在的问题

  1. 原始分数数据被破坏:search方法中将已处理候选人的分数设为0,导致后续无法读取剩余候选人的真实分数,比如测试用例1中C5的分数被覆盖,无法被检索。
  2. 并列排名处理逻辑错误:仅当并列人数>2时才用逗号连接候选人,遗漏2人并列的情况;且循环n次输出,导致同分场景下出现多余的无效排名行,比如测试用例2输出3行,实际只需1行。
  3. 分数格式不统一:部分分数输出为整数(如2),不符合预期的浮点数格式(如2.0)。

修正后的代码

# 标准答案
CORRECT_ANSWERS = ["A", "B", "B", "A", "C"]
# 存储候选人信息:(编号, 总分)
candidates = []

n = int(input())
for i in range(n):
    answers = input().split()
    score = 0.0
    for ans, correct in zip(answers, CORRECT_ANSWERS):
        if ans == correct:
            score += 2
        elif ans != "X":
            score -= 0.5
    candidates.append((f"C{i+1}", score))

# 按总分降序排序,总分相同则按编号升序
candidates.sort(key=lambda x: (-x[1], x[0]))

print("Rank Candidates Total")
rank = 1
i = 0
while i < n:
    current_score = candidates[i][1]
    # 收集所有同分的候选人
    same_score_group = []
    while i < n and candidates[i][1] == current_score:
        same_score_group.append(candidates[i][0])
        i += 1
    # 格式化输出内容
    candidates_str = ",".join(same_score_group)
    # 确保分数统一为浮点数格式
    score_str = f"{current_score:.1f}" if current_score % 1 != 0 else f"{current_score}.0"
    print(f"{rank} {candidates_str} {score_str}")
    # 跳过同分组人数更新排名
    rank += len(same_score_group)

修正说明

  1. 保留原始分数数据:通过排序+分组的方式处理排名,不再修改原始分数列表,避免数据丢失。
  2. 正确处理并列排名:遍历排序后的候选人列表,将所有同分候选人归为一组,一次输出该组的排名,排名值根据分组人数递增。
  3. 统一分数显示格式:使用格式化字符串确保分数始终以浮点数形式输出。
  4. 简化代码结构:移除不必要的类,优化逻辑,提升代码可读性。

内容的提问来源于stack exchange,提问作者starlitdreams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:35:16