開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIII部(ステートメント)の11章(代入ステートメント、式ステートメント、print(python3から関数))練習問題1.を解いてみる。
その他参考書籍
1.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- l = ["zero", 'one', 'two','three','four','five'] d = {5:"five", 4:"four", 3:"three", 2:"two", 1:"one"} for x in range(10): if x == 1: print("one") elif x == 2: print("two") elif x == 3: print("three") elif x == 4: print("four") elif x == 5: print("five") else: print("?") if x < len(l): print(l[x]) else: print("?") if x in d.keys(): print(d[x]) else: print("?") print()
入出力結果(Terminal)
$ ./sample.py ? zero ? one one one two two two three three three four four four five five five ? ? ? ? ? ? ? ? ? ? ? ? $
ちなみにJavaScriptの場合。
コード(BBEdit)
var a = ['zero', 'one', 'two', 'three', 'four', 'five'], d = {1:'one', 2:'two', 3:"three", 4:'four', 5:'five'}, result = "", i, max; for (i = 0, max = 10; i < max; i += 1) { if (i === 1) { result += "one\n"; } else if (i === 2) { result += "two\n"; } else if (i === 3) { result += "three\n"; } else if (i === 4) { result += "four\n"; } else if (i === 5) { result += "five\n"; } else { result += "?\n"; } if (a[i] !== undefined) { result += a[i] + "\n"; } else { result += "?\n"; } if (d[i] !== undefined) { result += d[i] + "\n"; } else { result += "?\n"; } result += "\n"; } $('#pre0').text(result);
0 コメント:
コメントを投稿