جواب «A Card From the Table» چالش 3 از جلسهی 15
سؤال
با input() اسم یک بازیکن را بگیر و امتیازهایش را از جدول scores (به ترتیب id) با sql و ? بخوان. از آنها یک سند بساز: {"name": اسم, "scores": [...]} که هر امتیاز در لیست scores یک دیکشنری {"game": ..., "score": ...} است، و با show چاپش کن. برای Ali:{"name": "Ali", "scores": [{"game": "Snake", "score": 95}, {"game": "Pong", "score": 42}]}
راهنما
for game, score in rows: → card["scores"].append({"game": game, "score": score}). آخر کار show(card).
یک جواب درست
from db import show, sql
name = input()
rows = sql("SELECT game, score FROM scores WHERE player = ? ORDER BY id", (name,))
card = {"name": name, "scores": []}
for game, score in rows:
card["scores"].append({"game": game, "score": score})
show(card)
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن