首页 > 解决方案 > 我如何分隔字符串中的某些单独的东西

问题描述

我有一个字符串,我想将其分成某些部分。

triangle = (input("Please input the dimensions of a triangle: "))

def comma(tri):

    comma = triangle.find(",")
    comma2 = triangle.find(",")
    side1 = (triangle[0:comma])
    side2 = (triangle[comma+1:comma2])
    side3 = (triangle[comma2:])
    return side1, side2, side3
print (comma(triangle))

我想做一些东西,通过输入边测量告诉你它是什么类型的三角形。我想知道如何在不使用拆分的情况下将测量值与输入分开。

标签: python

解决方案


这样输入会很麻烦。用户可以通过多种方式输入尺寸。相反,只需要求用户输入 3 次。然后只需将所有 3 个变量传递给一个函数。


side1 = (input("Please input the first dimension of a triangle: "))
side2 = (input("Please input the second dimension of a triangle: "))
side3 = (input("Please input the third dimension of a triangle: "))


推荐阅读