关于#python#的问题:用Python解答

img


题目简单,但是比较繁琐,用Python解答,最好可以加一点注释

(1)单分支

import math

x = float(input('x='))
y = math.sin(x) + 2*math.sqrt(x+math.e**4) - (x+1)**3
if x <0:
    y = math.log(-5*x,math.e) - math.fabs(x**2-8*x)/(7*x)+math.e

print(y)

(2)双分支

import math

x = float(input('x='))
y = 0
if x>=0 :
    y = math.sin(x) + 2*math.sqrt(x+math.e**4) - (x+1)**3
else:
    y = math.log(-5*x,math.e) - math.fabs(x**2-8*x)/(7*x)+math.e

print(y)

(3)三条件

import math

x = float(input('x='))
if x>0:
    y = math.sin(x) + 2*math.sqrt(x+math.e**4) - (x+1)**3
elif x <0:
    y = math.log(-5*x,math.e) - math.fabs(x**2-8*x)/(7*x)+math.e
else:
    y = 2*math.e-1 #这里把x=0的情况单独拿出来把公式简化一下

print(y)

import math # 导入数学包

def fun(x): # 单分支
    if x< 0:
        return math.log(-5 * x,math.e) - abs(x ** 2 - 8 * x) / (7 * x) + math.e
    return math.sin(x) + 2 * (x + math.e ** 4) ** .5 - (x + 1) ** 3

x = float(input())
print(fun(x))
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632