開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (Amit Saha (著)、No Starch Press)のChapter 1.(Working with Numbers)、Programming Challenges #2: Enhanced Multiplication Table Generator(No. 777)を解いてみる。
#2: Enhanced Multiplication Table Generator(No. 777)
コード(Emacs)
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def multi_table(a, b):
for n in range(1, a + 1):
print('{0} x {1} = {2}'.format(b, n, b * n))
if __name__ == '__main__':
while True:
a = input('Enter a number(listing): ')
if a == 'q':
break
b = input('Enter a number(nultiple):')
multi_table(int(a), float(b))
入出力結果(Terminal, IPython)
$ ./sample2.py Enter a number(listing): 15 Enter a number(nultiple):9 9.0 x 1 = 9.0 9.0 x 2 = 18.0 9.0 x 3 = 27.0 9.0 x 4 = 36.0 9.0 x 5 = 45.0 9.0 x 6 = 54.0 9.0 x 7 = 63.0 9.0 x 8 = 72.0 9.0 x 9 = 81.0 9.0 x 10 = 90.0 9.0 x 11 = 99.0 9.0 x 12 = 108.0 9.0 x 13 = 117.0 9.0 x 14 = 126.0 9.0 x 15 = 135.0 Enter a number(listing): 10 Enter a number(nultiple):20 20.0 x 1 = 20.0 20.0 x 2 = 40.0 20.0 x 3 = 60.0 20.0 x 4 = 80.0 20.0 x 5 = 100.0 20.0 x 6 = 120.0 20.0 x 7 = 140.0 20.0 x 8 = 160.0 20.0 x 9 = 180.0 20.0 x 10 = 200.0 Enter a number(listing): q $
0 コメント:
コメントを投稿