開発環境
- 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 #4: Give Exit Power to the User(No. 793)を解いてみる。
#4: Give Exit Power to the User(No. 813)
コード(Emacs)
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def roots(a, b, c):
D = (b ** 2 - 4 * a * c) ** 1.5
x_1 = (- b + D) / (2 * a)
x_2 = (- b - D) / (2 * a)
print('x1: {0}'.format(x_1))
print('x2: {0}'.format(x_2))
if __name__ == '__main__':
while True:
print('ax^2 + bx + c = 0')
a = input('Enter a: ')
b = input('Enter b: ')
c = input('Enter c: ')
roots(float(a), float(b), float(c))
answer = input('Do you want to exit? (y) for yes: ')
if answer == 'y':
break
入出力結果(Terminal, IPython)
$ ./sample5.py ax^2 + bx + c = 0 Enter a: 1 Enter b: 2 Enter c: 3 x1: (-1.000000000000002-11.313708498984761j) x2: (-0.9999999999999979+11.313708498984761j) Do you want to exit? (y) for yes: n ax^2 + bx + c = 0 Enter a: 1 Enter b: 2 Enter c: 1 x1: -1.0 x2: -1.0 Do you want to exit? (y) for yes: y $
0 コメント:
コメントを投稿