Discuss / Python / 最后一行 r = quadratic(t1, t2, t3) 有些小疑问

最后一行 r = quadratic(t1, t2, t3) 有些小疑问

Topic source
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import math

def quadratic(a, b, c):
    if not isinstance(a, (int, float)):
        raise TypeError('bad operand type')
    if not isinstance(b, (int, float)):
        raise TypeError('bad operand type')
    if not isinstance(b, (int, float)):
        raise TypeError('bad operand type')

    delta = b * b - 4 * a * c
    if delta >= 0:
        e = math.sqrt(delta)
        m = (-b + e) / (2 * a)
        n = (-b - e) / (2 * a)
        print('The equation\'s roots are...\nx1 = %.1f , x2 = %.1f ...' % (m, n))
        return m,n
    else:
        print('There is no root...')
        pass

print('please enter the a, b, c of the equation: ax2 + bx + c = 0')
t1 = float(input('a = ',))
t2 = float(input('b = ',))
t3 = float(input('c = ',))

r = quadratic(t1, t2, t3)

最后一行,用 r = ,如果方程没有解,就运行正常, 如果写成 x1,x2 = , 如果方程没有解,就NoneType object is not iterable ... 求解释...

小丑J2

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

因为x1, x2只在定义的函数内部有效,在函数外部是没有这两个变量的,不能直接调用


  • 1

Reply