Discuss / Python / 作业

作业

Topic source

Deadstring

#1 Created at ... [Delete] [Delete and Lock User]
import math
def quadratic(a, b, c):
    m = b**2-4ac
    if m < 0:
        return 'NO RESULT'
    else:
        x1 = (-b + math.sqrt(m))/(2*a)
        x2 = (-b - math.sqrt(m))/(2*a)
        return x1, x2
print('Calculate the roots of ax^2+bx+c=0')
a = float(input('Please input a:'))
b = float(input('Please input b:'))
c = float(input('Pleasw input c:'))
roots = quadratic(a, b, c)
print('The answer is:', roots)

没问题是没问题,不过为什么不能直接 print('The answer is:',quadratic(a, b, c) ) 或者 print('The answer is %.2f',% quadratic(a, b, c) )呢?

Deadstring

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

改成m = b*2-4a*c

土城剑客

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

1、

没问题是没问题,不过为什么不能直接
print('The answer is:',quadratic(a, b, c) )
或者
print('The answer is %.2f',% quadratic(a, b, c) )呢?

print('The answer is %.2f',% quadratic(a, b, c) ) 这样当然是不行的,,号会被编译成连接符,当然不行,这点不像C语言正确的格式应该这样:

print('The answer is:(%.1f,%.1f)' % quadratic(a, b, c))

2 m应该修改为m = b**2-4*a*c


  • 1

Reply