Discuss / Python / 有疑问。。。

有疑问。。。

Topic source

HOMO-SUM

#1 Created at ... [Delete] [Delete and Lock User]
#!/usr/bin/env python
# encoding: utf-8

import math

def quadratic(a,b,c):
    ''' Solving a quadratic equation,
    giving the values of a, b, c
    (a, b, c the need for integral or floating-point type),
    and then calculate the equation of a*x*x + b*x + c = 0.
    '''
    for TestNumber in (a, b, c):
        if not isinstance(TestNumber, (int, float)):
            raise TypeError('bad operand type')
    if a == 0:
        print("Value a cannot be '0'!")
    elif b*b - 4*a*c >= 0:
        print("Solving the equation")
        values1 =(-b + math.sqrt(b*b - 4*a*c)) / 2*a
        values2 =(-b - math.sqrt(b*b - 4*a*c)) / 2*a
        print("The equation values1 is: %s, values2 is: %s" % (values1,values2))
    elif b*b - 4*a*c < 0:
        print("No solution equation")


try:
    a, b, c = map(float, input("Enter a, b, c:").split(',')[:3])
except ValueError:
    print("Wrrong Values")
quadratic(a,b,c)

本来输入想用下面这种方法:

numbers  = ''' Please input values a:
Please input values b:
Please input values c: '''
a, b, c = map(input, numbers.split('\n'))

但是不知道怎么和:

floats = [float(x) for x in numbers.split()]

结合。。。。

HOMO-SUM

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

写错了,忘记优先级了。。。

#!/usr/bin/env python
# encoding: utf-8

import math

def quadratic(a,b,c):
    ''' Solving a quadratic equation,
    giving the values of a, b, c
    (a, b, c the need for integral or floating-point type),
    and then calculate the equation of a*x*x + b*x + c = 0.
    '''

    TestEquation = b**2 - 4*a*c
    for TestNumber in (a, b, c):
        if not isinstance(TestNumber, (int, float)):
            raise TypeError('bad operand type')
    if a == 0:
        print("Value a cannot be '0'!")
    elif TestEquation >= 0:
        print("Solving the equation")
        values1 =(math.sqrt(TestEquation) + b) / (2*a)
        values2 =(math.sqrt(TestEquation) - b) / (2*a)
        print("The equation values1 is: %s, values2 is: %s" % (values1,values2))
    else:
        print("No solution equation")


try:
    a, b, c = map(float, input("Enter a, b, c:").split(',')[:3])
    print(a,b,c)
except ValueError:
    print("Wrrong Values")
quadratic(a,b,c)

  • 1

Reply