python简单的分段函数

img


分段函数然后这个上角标啥的都没哦明白
这个是学习用的小程序,不用写的很复杂。基础就行,我也是刚学。

import math

x = float(input(">>>"))
if x >= 3:    
    y = math.e ** x + x ** 0.5
elif x == 0:
    y = math.sin(x) + math.log2(1 + x)
else:
    y = -1

y = f"{y:.2f}"
print(y)

你题目的解答代码如下:

import math
x = float(input())
if x>=3:
    y = math.exp(x)+x**(1/2)
elif x==0:
    y = math.sin(x)+math.log2(1+x)
elif x<0:
    y = -1
else:
    y = 0

print(f'{y:.2f}')

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img