当我运行代码时,前 5 个失败,但最后两个通过

问题描述 投票:0回答:1

在老师提供的区域运行我的代码时,我收到输出

check.py solid: FAILED; expected solid,
saw liquid
check.py s1: FAILED; expected slush, saw liquid
check.py s2: FAILED; expected slush,
saw liquid
check.py s3: FAILED; expected slush, saw liquid
check.py s4: FAILED; expected slush, saw liquid
check.py 11: PASSED
check.py 12: PASSED

但是,当我尝试在 python 中运行它时,我不断收到 SyntaxError: invalid syntax 并且 check.expect("solid", Phase(10.0, 576.0), "solid") 中的 c 不断突出显示红色。我已经尝试了很多事情,但感觉我想太多了,错过了显而易见的事情。

def phase(si, temperature):
    if si < 0 or si > 100:
        return "Invalid si percentage (must be between 0 and 100)"
    
    if temperature < 0:
        return "Invalid temperature (must be greater than or equal to 0)"
    
    # Define phase transition boundaries
    solid_boundary = 0.15
    liquid_boundary = 0.85
    
    if temperature <= 0:
        return "solid"
    elif temperature >= 100:
        return "liquid"
    elif si <= solid_boundary:
        return "solid"
    elif si >= liquid_boundary:
        return "liquid"
    else:
        return "slush"

check.expect("solid", phase(10.0, 576.0), "solid")
check.expect("slush", phase(17.0, 577.1), "slush")
check.expect("slush", phase(18.0, 577.1), "slush")
check.expect("slush", phase(0.0, 660.0), "slush")
check.expect("slush", phase(100.0, 1410.3), "slush")
check.expect("liquid", phase(0.0, 660.4), "liquid")
check.expect("liquid", phase(100.0, 1410.5), "liquid")

我的作业是:写一个函数phase(s1,温度)。它采用 0.0 到 100.0 之间的数字来指示混合物中硅的百分比,以及以 °C 为单位的温度。该函数应返回“solid”、“Slush”或“Liquid”,指示混合物位于哪个区域。我已经尝试了上面的代码,并且

def phase(si, temperature):
    if si < 0 or si > 100:
        return "Invalid si percentage (must be between 0 and 100)"
    
    if temperature < 0:
        return "Invalid temperature (must be greater than or equal to 0)"
    
    # Define phase transition boundaries
    solid_boundary = 0.15
    liquid_boundary = 0.85
    
    if temperature <= 0:
        return "solid"
    elif temperature >= 100:
        return "liquid"
    elif si <= solid_boundary:
        return "solid"
    elif si >= liquid_boundary:
        return "liquid"
    else:
        return "slush"

check.expect("solid", phase(10.0, 576.0), "solid")
check.expect("slush", phase(17.0, 577.1), "slush")
check.expect("slush", phase(18.0, 577.1), "slush")
check.expect("slush", phase(0.0, 660.0), "slush")
check.expect("slush", phase(100.0, 1410.3), "slush")
check.expect("liquid", phase(0.0, 660.4), "liquid")
check.expect("liquid", phase(100.0, 1410.5), "liquid")

但不断收到相同的错误。

python syntax-error
1个回答
0
投票

您收到错误,因为您的代码没有定义“检查”函数,因此您正在调用一个不存在的函数。

无论您的老师使用什么来运行您的代码,确实在某处定义了检查函数(尽管您可能看不到它),但您的代码本身却没有。

© www.soinside.com 2019 - 2024. All rights reserved.