Python-100-Days/Day01-15/code/Day03/piecewise.py

20 lines
292 B
Python
Raw Normal View History

2018-03-02 00:09:06 +08:00
"""
分段函数求值
2019-05-03 21:17:36 +08:00
3x - 5 (x > 1)
f(x) = x + 2 (-1 <= x <= 1)
5x + 3 (x < -1)
2018-03-02 00:09:06 +08:00
Version: 0.1
Author: 骆昊
Date: 2018-02-28
"""
x = float(input('x = '))
if x > 1:
2019-05-03 21:17:36 +08:00
y = 3 * x - 5
2018-03-02 00:09:06 +08:00
elif x >= -1:
2019-05-03 21:17:36 +08:00
y = x + 2
2018-03-02 00:09:06 +08:00
else:
2019-05-03 21:17:36 +08:00
y = 5 * x + 3
2018-03-02 00:09:06 +08:00
print('f(%.2f) = %.2f' % (x, y))