Python if × and 演算子 実践問題集(初心者向け)
「条件を満たしたらこうする」という場面はプログラミングでよく出てきます。ここでは if と and を組み合わせた練習問題を用意しました。実際の生活シーンを題材にしているので、イメージしやすいはずです。
問題1:図書館の入館条件
- 条件: 「会員カードを持っていて、かつ返却期限を過ぎていない人だけ入館できる」
has_card = True
overdue = False
if has_card and (not overdue):
print("入館できます")
else:
print("入館できません")
Python問題2:ネットショッピングの送料無料
- 条件: 「会員で、かつ購入金額が5000円以上なら送料無料」
is_member = True
amount = 4800
if is_member and (amount >= 5000):
print("送料無料です")
else:
print("送料がかかります")
Python問題3:試験合格判定
- 条件: 「数学が60点以上、かつ英語が60点以上なら合格」
math = 70
english = 55
if (math >= 60) and (english >= 60):
print("合格")
else:
print("不合格")
Python問題4:イベント参加条件
- 条件: 「年齢が18歳以上、かつ同意書にサインしている人だけ参加可能」
age = 20
signed = False
if (age >= 18) and signed:
print("参加できます")
else:
print("参加できません")
Python問題5:パソコンのログイン
- 条件: 「ユーザー名が正しい、かつパスワードが正しい」
username_ok = True
password_ok = True
if username_ok and password_ok:
print("ログイン成功")
else:
print("ログイン失敗")
Python問題6:スポーツ大会の出場条件
- 条件: 「体重が50kg以上、かつ健康診断で問題なし」
weight = 48
healthy = True
if (weight >= 50) and healthy:
print("出場できます")
else:
print("出場できません")
Python練習のポイント
- 「かつ」の条件を考える: 両方満たす必要があるかどうかを意識する
- 丸括弧で見やすく:
(条件1) and (条件2)の形にすると読みやすい - 生活シーンに置き換える: 「入館条件」「割引条件」など身近な例で練習すると理解が早い
