名称错误:名称“xone”未定义(不要看俄语单词,它不负责任何事情

2024-09-29 21:40:20 发布

您现在位置:Python中文网/ 问答频道 /正文

回溯(最近一次呼叫最后一次): 文件“DS”,第22行,在 打印(“x1=”+str(xone)) NameError:未定义名称“xone”

a、 b,cêD

a=浮动(输入(“bччччччччa:”)

b=浮动(输入(“bчччччччb:”)

c=浮动(输入(“bчччччччc:”)

发现D

D=(b*b)-4*a*c

查找x1,x2并打印此

如果D>;0:

xone=(-b+(D/D))/2*a

xtwo=(-b-(D/D))/2*a

打印(“D=“+str(D))

打印(“x1=”+str(xone))

打印(“x2=“+str(xtwo))

发现x

如果D==0:

x=-b/(2*a)

打印(“D=“+str(D))

打印(“x=”+str(x))

如果D<;0:

打印(“D=“+str(D))

打印(“没有x1和x2!”)


Tags: 文件ltgt名称dsx1x2未定义
1条回答
网友
1楼 · 发布于 2024-09-29 21:40:20

你遇到了问题

if D > 0:
    xone = ( -b + (D / D)) / 2 * a 

在D不大于0的情况下,xone没有定义。因此,当您到达第22行的print ( " x1 = " + str(xone))时,xone没有定义,因此给出了这个NameError

建议:

if D > 0:
    xone = ( -b + (D / D)) / 2 * a 
else:
    xone = {something else}

建议2,保持代码样式不变:

D = (b * b) - 4 * a * c

if D > 0:
    xone = ( -b + (D / D)) / 2 * a 
    xtwo = ( -b - (D / D)) / 2 * a 
    print ( " D = " + str(D))
    print ( " x1 = " + str(xone))
    print ( " x2 = " + str(xtwo))

elif D < 0:
    x = 0
    print("something")

else:
    # This will also execute if D is a string or something else, which potentially causes problems with the next line
    xthree = -b / (2 * a)
    print ( " D = " + str(D))
    print ( " x3 = " + str(xthree ))

相关问题 更多 >

    热门问题