ヒント_2_7

ヒント:練習問題_2_7(if~elif~else文+ブール演算 丸い的)

x, y にそれぞれ 0 ~ 120, 0 ~ 80 の float または int を代入します。
ここでは条件が複雑になるので bool 型の変数を定義して進めます。

中心が (60, 30)の円をA、(45, 50)の円をB、(75, 50)の円をCと仮定したとき、
矢が A, B, C の円内にある時にTrueとなる bool 変数をそれぞれ in_A, in_B, in_C と定義します。すると

in_A = ((x - 60) ** 2 + (y - 30) ** 2) ** 0.5 <= 20.0
in_B = ((x - 45) ** 2 + (y - 50) ** 2) ** 0.5 <= 20.0
in_C = ((x - 75) ** 2 + (y - 50) ** 2) ** 0.5 <= 20.0

となります。
次に 点数が50, 30, 10の領域で True となる bool 変数をそれぞれ in_50, in_30, in_10 とすると

in_30 = in_A and in_B and in_C
in_20 = (in_A and in_B and not in_C) or (in_B and in_C and not in_A) or (in_C and in_A and not in_B)
in_10 = (in_A and not in_B and not in_C) or (in_B and not in_C and not in_A) or (in_C and not in_A and not in_B)

あとは、in_50, in_30, in_10 を条件として使用して if 文を作成します。

もし in_50 が True の場合
    score = 50
それ以外でもし in_30 が True の場合
    score = 30
それ以外でもし in_10 が True の場合
    score = 10
それ以外
    score = 0

('score: ', score) を出力します

解答例はこちら

タイトルとURLをコピーしました