我需要从逗号混合的字符串中挑出一些数字

时间:2015-02-18 03:09:44

标签: python string python-2.7 input max

所以我做了一个小小的右三角检查器

    def RightTriangleChecker(Number1,Number2,Number3):
        AllNumbers=[Number1,Number2,Number3]
        Hypotonuse=max(AllNumbers)
        if(Number1**2+Number2**2==Number3**2):
            print "This is a right triangle."
        else:
           print"This is not a right triangle."

问题是当我得到数字时,我得到一个数字,逗号和另一个数字。或者那些的一些组合(取决于输入)我想知道是否有人可以帮我找到一种方法来获得数字。

2 个答案:

答案 0 :(得分:2)

s = "26, 43, 25"
nums = [int(i) for i in s.split(",")]

另外,我会把你的功能重写为

def is_right_triangle(x, y, z):
    a, b, c = sorted([x, y, z])
    return a*a + b*b == c*c

答案 1 :(得分:0)

    def RightTriangleChecker(Number1,Number2,Number3):
AllNumbers=[Number1,Number2,Number3]
Hypotonuse=max(AllNumbers)
if(Number1**2+Number2**2==Number3**2):
    print "This is a right triangle."
else:
    print"This is not a right triangle."


    def Retry():
        Answer==raw_input("Retry?")
        if (Answer=="Yes"):
            TriangleLength=raw_input("What are the lengths of the sides?         (Please put commas in-between each number)")
       RightTriangleChecker(TriangleLength[1],TriangleLength[2],TriangleLength[3])
else:
    exit(0)

    Original=raw_input("Would you like to use this program?")
    if (Original=="Yes"):
            TriangleLength=raw_input("What are the lengths of the sides? (Please put commas in-between each number)")
        RightTriangleChecker(TriangleLength[1],TriangleLength[2],TriangleLength[3])
    if (Original=="No"):
            exit(0)
    Retry()