Learning to program - The Basics - Branching

途中で作ったプログラムifminmax.py

value = int(raw_input(""))
maximum = 100
minimum = 5
if value > maximum:
    print "Value is out of range!(over maximum)"
elif value < minimum:
    print "Value is out of range!(under minimum)"
else:
    print "Value within range!" 

プログラムの名前はifMinMax.pyの方がよかったのかな。
int()で囲まないとPythonはデータとして変数を保持しない。
else value < minimum:
ってやってたんだけど、elseの後はごちゃごちゃつけたらあかんってことがわかった。if,elifなどで当てはまらなかった場合全て渡される値としてelseがあるみたいな感じだからか。
後、練習中なので(over ~)とかつけておかないと全部同じ文言がきて何がなんだかわからなくなるw
elifはまぁ、後で学ぶでしょう。事前にチュートリアルやってたからここまである程度知ってたわけだが。
C言語と同じで事前にやってるっていうのはある程度はかどるもんなんですね。if文とかroop文とかメモリにジャンプするぜ!とかもそれなりに初見で理解できるし。つうか事前にやってた時点で初見じゃないんですが。
これもテストだか評価文だか条件文だか知らんが、そこを一行でまとめられるのかなぁ。後で試してみよう。まぁ、文言変えている所為で行き着かせたいところが三つ以上に分かれてるからおそらく無理か。

Note 2:A subtle point to notice is that we perform the greater-than tests from the highest value down to the lowest. If we did it the other way round the first test, which would be price > 200 would always be true and we would never progress to the > 500 test. Similarly if using a sequence of less-than tests you must start at the lowest value and work up. This is another very easy trap to fall into.

入れ子?Chainingにするときの注意点。

JavascriptのSwitch文にCircleを導入したソース。

<html>
<body>
<script type="text/javascript">
function doArea(){
   var shape, breadth, length, area;
   shape   = document.area.shape.value;
   breadth = parseInt(document.area.breadth.value);
   len     = parseInt(document.area.len.value);
   radius  = parseInt(document.area.radius.value);
   switch (shape){
       case 'Square': 
           area = len * len;
           alert("Area of " + shape + " = " + area);
           break;
       case 'Rectangle': 
           area = len * breadth;
           alert("Area of " + shape + " = " + area);
           break;
       case 'Triangle':
           area = len * breadth / 2;
           alert("Area of " + shape + " = " + area);
           break;
       case 'Circle':
           area = radius * radius * 3.14
           alert("Area of " + shape + " = " + area);
           break;
       default: alert("No shape matching: " + shape)
       };
}
</script>

<form name="area">
Length:  <input type="text" name="len">
Breadth: <input type="text" name="breadth">
Radius: <input type="text" name="radius">
Shape:   <select name="shape" size=1 onChange="doArea()">
           <option value="Square">Square
           <option value="Rectangle">Rectangle
           <option value="Triangle">Triangle
           <option value="Circle">Circle
         </select>
</form>

</body>
</html>

The thing to note here is that we do not increment the index if we remove an item, we rely on the deletion moving everything up so that the old index value now points at the next item in the collection. We use an if/else branch to control when we increment the index. It's very easy to make a mistake doing this kind of thing so test your code carefully.

確かにプログラミングにはこういう点があるので気をつけないといけない。コードをしっかり読まないと陥る。(この場合ループ文の反芻が必要。)

さぁ、終わったぞー、条件文も!結構時間掛かったけど次だ次。

はてな記法にプログラムごとハイライトとかされる無茶便利な記法があることを知った。なにこれ素敵。ぷれぷれで挟んではてな糞だなとか思ってて本当にごめんなさい。平身低頭。
ソースコードを色付けして記述する(シンタックス・ハイライト) - はてなダイアリーのヘルプ
というわけで今度からこの記法を使っていくことにする。
まだ改行・pタグ周りの関係に不満があるけどそれでも大分見栄えがよくなったなぁ。なんかちょっとプログラマのブログに見えるし。見えないって?ははは