例えば
1 ⇒ A
6 ⇒ F
最初に大変な例を
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
#1~10の数字が入力されるとき、その数字番目のアルファベットを表示 | |
#5 ⇒ E | |
num = int(input()) | |
if num == 1: | |
print("A") | |
elif num == 2: | |
print("B") | |
elif num == 3: | |
print("C") | |
elif num == 4: | |
print("D") | |
elif num == 5: | |
print("E") | |
elif num == 6: | |
print("F") | |
elif num == 7: | |
print("G") | |
elif num == 8: | |
print("H") | |
elif num == 9: | |
print("I") | |
else: | |
print("J") |
次にいささか短くした例を
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
#1~10の数字が入力されるとき、その数字番目のアルファベットを表示 | |
#5 ⇒ E | |
a = "ABCDEFGHIJ" | |
al = list(a) | |
num = int(input()-1) | |
print(al[num]) |
だいぶ短くなりました。
ある意味では最初の例はシンプルで、入門者(プログラム未経験者)でもコードでやりたいことがわかるという、非常に理解しやすいコードだと思います。
対して2番目のコードは少なくともリストの形を知らないとわかりにくいですが、プログラムのコードは4行に抑えられました。
0 件のコメント:
コメントを投稿