読み方のヒント
矢印ボタン(または ← → キー)でページを移動できます。
次は、「予約できて、300円以下の商品」を選びます。2つの条件を両方とも満たす商品だけを表示するには、and を使います。
from products import products
for product in products:
if product["reservable"] and product["price"] <= 300:
print(product["id"], product["name"], product["price"])
p002 クロワッサン 220
p003 あんパン 180
p004 カレーパン 240
p005 メロンパン 200
p008 ベーコンエピ 280
| 書き方 | 意味 |
|---|---|
条件1 and 条件2 | 両方とも True のときだけ True |
条件1 or 条件2 | どちらか一方でも True なら True |
p001(380円)と p007(320円)は300円を超えるため、p006 は予約できないため、表示されません。
結果は products の並び順のままです。価格の安い順にはなっていません。条件で選ぶ処理と、並べ替える処理は別の処理です。
if の行を if product["category"] == "bread": にし、print(product["name"], product["price"]) で表示します。食パン(1斤)380、あんパン 180、バゲット 320 の3つが表示されます。文字列を比べるときも == を使います。
19 / 20