Discuss / Python / 加了检验input value的格式

加了检验input value的格式

Topic source

Jonathon_C

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

import math

def quadratic(a, b, c):

    # Check if a, b and c are valid number

    if not isinstance(a, (int, float)):

        return TypeError('Please enter a valid number for a!')

    if not isinstance(b, (int, float)):

        return TypeError('Please enter a valid number for b!')

    if not isinstance(b, (int, float)):

        return TypeError('Please enter a valid number for c!')

    x1 = (-b + math.sqrt(b*b - 4 * a * c)) / (2 * a)

    x2 = (-b - math.sqrt(b*b - 4 * a * c)) / (2 * a)

    return print('This unary quadratic equation has two outputs. x1 is %.2f, and x2 is %.2f.' % (x1, x2))

print(quadratic(1, 3, -4))


  • 1

Reply