找出这个辅助函数为什么会接受不符合整数分值契约的值。 def record_cents ( record : dict [ str , object ] ) -> int :
cents = record . get ( "cents" ) or 0
if not isinstance (cents, int ):
raise ValueError ( "cents must be an integer" )
return cents
查看答案
上一道练习 审查生成的订单总额 审查AI代码
问问你的AI 让你的AI把这段代码改成引导式找错练习。
Claude ChatGPT 复制提示词 I am reading "找出这个辅助函数为什么会接受不符合整数分值契约的值。" on CodeWiki (https://codewiki.com/zh/practice/spotbug/python/variables-data-types/spotbug-cents-validation/).
Context (verbatim from the page):
"""
def record_cents(record: dict[str, object]) -> int:
cents = record.get("cents") or 0
if not isinstance(cents, int):
raise ValueError("cents must be an integer")
return cents
"""
Introduce three subtle bugs into the example above and let me find them. Reveal each answer only after I have guessed.
Answer in 中文. Keep code examples in python. Where you are unsure, say so.