Discuss / Python / 为什么这个不行

为什么这个不行

Topic source

Cuber陈

#1 Created at ... [Delete] [Delete and Lock User]
import math
def quadratic(a,b,c):
	m=b*b-4*a*c
	if m<0:
		return 'error'
	elif m=0:
		x=-b/(2*a)
		return x
	else m>0:
		x1=(-b+math.sqrt(m))/(2*a)
		x2=(-b-math.sqrt(m))/(2*a)
		return x1,x2
a=input('please input a:')
b=input('please input b:')
c=input('please input c:')
a=float(a)
b=float(b)
c=float(c)
x=quadratic(a,b,c)
print('x=',x)

第六行报错

Cuber陈

#2 Created at ... [Delete] [Delete and Lock User]

把m=0的条件放到最后边就可以了:

import math
def quadratic(a,b,c):
	m=b*b-4*a*c
	if m<0:
		return 'error'
	elif m>0:
		x1=(-b+math.sqrt(m))/(2*a)
		x2=(-b-math.sqrt(m))/(2*a)
		return x1,x2
	else:
		x=-b/(2*a)
		return x
a=input('please input a:')
b=input('please input b:')
c=input('please input c:')
a=float(a)
b=float(b)
c=float(c)
x=quadratic(a,b,c)
print('x=',x)

ErinBZ99

#3 Created at ... [Delete] [Delete and Lock User]

表示比较关系时,用== ,一个=表示赋值


  • 1

Reply