جواب «Read the Decisions» چالش 2 از جلسهی 73
سؤال
هر تصمیم یک متن JSON است. آن را باز کن و چاپ کن: THINK: <thought> و بعد اگر final داشت FINAL: <final>، وگرنه ACT: <action>(<input>). خروجی:THINK: I need the diameter of Mars.ACT: lookup(Mars)THINK: Subtract them.ACT: calc(12742 - 6779)THINK: I have everything.FINAL: Earth is bigger by 5963 km.
راهنما
d = json.loads(text)؛ print("THINK:", d["thought"])؛ if "final" in d: ... else: print(f"ACT: {d['action']}({d['input']})").
یک جواب درست
import json
decisions = [
'{"thought": "I need the diameter of Mars.", "action": "lookup", "input": "Mars"}',
'{"thought": "Subtract them.", "action": "calc", "input": "12742 - 6779"}',
'{"thought": "I have everything.", "final": "Earth is bigger by 5963 km."}',
]
for text in decisions:
d = json.loads(text)
print("THINK:", d["thought"])
if "final" in d:
print("FINAL:", d["final"])
else:
print(f"ACT: {d['action']}({d['input']})")
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن