{"id":1141832,"date":"2025-01-08T22:34:33","date_gmt":"2025-01-08T14:34:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141832.html"},"modified":"2025-01-08T22:34:36","modified_gmt":"2025-01-08T14:34:36","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e7%ad%89%e8%85%b0%e4%b8%89%e8%a7%92%e5%bd%a2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141832.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u7b49\u8170\u4e09\u89d2\u5f62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104429\/29ec8452-908d-451b-9baf-66e65e7b72f9.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u7b49\u8170\u4e09\u89d2\u5f62\" \/><\/p>\n<p><p> <strong>Python\u5224\u65ad\u7b49\u8170\u4e09\u89d2\u5f62\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5229\u7528\u4e24\u8fb9\u76f8\u7b49\u7684\u7279\u6027\u3001\u8ba1\u7b97\u6240\u6709\u8fb9\u957f\u5e76\u8fdb\u884c\u6bd4\u8f83\u3001\u4f7f\u7528math\u5e93\u8fdb\u884c\u7cbe\u786e\u8ba1\u7b97\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u5229\u7528\u4e24\u8fb9\u76f8\u7b49\u7684\u7279\u6027<\/strong> \u662f\u6700\u76f4\u89c2\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u4e09\u89d2\u5f62\u7684\u4e09\u6761\u8fb9\uff0c\u82e5\u6709\u4e24\u6761\u8fb9\u76f8\u7b49\uff0c\u5219\u8be5\u4e09\u89d2\u5f62\u4e3a\u7b49\u8170\u4e09\u89d2\u5f62\u3002\u4f8b\u5982\uff0c\u8bbe\u4e09\u89d2\u5f62\u7684\u4e09\u6761\u8fb9\u4e3aa\u3001b\u3001c\uff0c\u90a3\u4e48\u53ea\u9700\u5224\u65ada\u662f\u5426\u7b49\u4e8eb\uff0c\u6216\u8005b\u662f\u5426\u7b49\u4e8ec\uff0c\u6216\u8005a\u662f\u5426\u7b49\u4e8ec\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5229\u7528\u4e24\u8fb9\u76f8\u7b49\u7684\u7279\u6027<\/h3>\n<\/p>\n<p><p>\u5224\u65ad\u7b49\u8170\u4e09\u89d2\u5f62\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\u5229\u7528\u5176\u5b9a\u4e49\uff1a\u5373\u6709\u4e24\u8fb9\u76f8\u7b49\u7684\u4e09\u89d2\u5f62\u3002\u4ee5\u4e0b\u662fPython\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_isosceles_triangle(a, b, c):<\/p>\n<p>    if a == b or b == c or a == c:<\/p>\n<p>        return True<\/p>\n<p>    else:<\/p>\n<p>        return False<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>a, b, c = 5, 5, 8<\/p>\n<p>print(is_isosceles_triangle(a, b, c))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8ba1\u7b97\u6240\u6709\u8fb9\u957f\u5e76\u8fdb\u884c\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5148\u8ba1\u7b97\u51fa\u4e09\u89d2\u5f62\u7684\u4e09\u6761\u8fb9\u957f\uff0c\u7136\u540e\u8fdb\u884c\u6bd4\u8f83\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e09\u70b9A(x1, y1)\u3001B(x2, y2)\u3001C(x3, y3)\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8ba1\u7b97\u8fb9\u957f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8ba1\u7b97\u8fb9\u957f<\/strong>\uff1a\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\u8ba1\u7b97\u8fb9\u957f\u3002<\/li>\n<li><strong>\u6bd4\u8f83\u8fb9\u957f<\/strong>\uff1a\u6bd4\u8f83\u662f\u5426\u6709\u4e24\u6761\u8fb9\u76f8\u7b49\u3002<\/li>\n<\/ol>\n<p><h4>1. \u8ba1\u7b97\u8fb9\u957f<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\u8ba1\u7b97\u4e09\u89d2\u5f62\u7684\u4e09\u6761\u8fb9\u957f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def distance(x1, y1, x2, y2):<\/p>\n<p>    return math.sqrt((x2 - x1) &lt;strong&gt; 2 + (y2 - y1) &lt;\/strong&gt; 2)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>x1, y1 = 0, 0<\/p>\n<p>x2, y2 = 5, 0<\/p>\n<p>x3, y3 = 2.5, 5<\/p>\n<p>a = distance(x1, y1, x2, y2)<\/p>\n<p>b = distance(x2, y2, x3, y3)<\/p>\n<p>c = distance(x1, y1, x3, y3)<\/p>\n<p>print(a, b, c)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6bd4\u8f83\u8fb9\u957f<\/h4>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4e0a\u8ff0\u7684<code>is_isosceles_triangle<\/code>\u51fd\u6570\u8fdb\u884c\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_isosceles_triangle_from_points(x1, y1, x2, y2, x3, y3):<\/p>\n<p>    a = distance(x1, y1, x2, y2)<\/p>\n<p>    b = distance(x2, y2, x3, y3)<\/p>\n<p>    c = distance(x1, y1, x3, y3)<\/p>\n<p>    return is_isosceles_triangle(a, b, c)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>print(is_isosceles_triangle_from_points(0, 0, 5, 0, 2.5, 5))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528math\u5e93\u8fdb\u884c\u7cbe\u786e\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6d6e\u70b9\u6570\u6bd4\u8f83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bef\u5dee\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528<code>math.isclose<\/code>\u51fd\u6570\u8fdb\u884c\u7cbe\u786e\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_isosceles_triangle_precise(a, b, c, tolerance=1e-9):<\/p>\n<p>    if math.isclose(a, b, abs_tol=tolerance) or math.isclose(b, c, abs_tol=tolerance) or math.isclose(a, c, abs_tol=tolerance):<\/p>\n<p>        return True<\/p>\n<p>    else:<\/p>\n<p>        return False<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>a, b, c = 5.000000001, 5, 8<\/p>\n<p>print(is_isosceles_triangle_precise(a, b, c))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5224\u65ad\u4e00\u4e2a\u4e09\u89d2\u5f62\u662f\u5426\u4e3a\u7b49\u8170\u4e09\u89d2\u5f62\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u5229\u7528\u4e24\u8fb9\u76f8\u7b49\u7684\u7279\u6027\uff0c\u8ba1\u7b97\u6240\u6709\u8fb9\u957f\u5e76\u8fdb\u884c\u6bd4\u8f83\uff0c\u6216\u4f7f\u7528math\u5e93\u8fdb\u884c\u7cbe\u786e\u8ba1\u7b97\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5229\u7528\u4e24\u8fb9\u76f8\u7b49\u7684\u7279\u6027<\/strong>\uff1a\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5df2\u77e5\u4e09\u8fb9\u957f\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u8ba1\u7b97\u6240\u6709\u8fb9\u957f\u5e76\u8fdb\u884c\u6bd4\u8f83<\/strong>\uff1a\u9002\u7528\u4e8e\u5df2\u77e5\u9876\u70b9\u5750\u6807\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u4f7f\u7528math\u5e93\u8fdb\u884c\u7cbe\u786e\u8ba1\u7b97<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5224\u65ad\u4e00\u4e2a\u4e09\u89d2\u5f62\u662f\u5426\u4e3a\u7b49\u8170\u4e09\u89d2\u5f62\uff0c\u5e76\u5e94\u7528\u4e8e\u5404\u79cd\u5b9e\u9645\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u5224\u65ad\u4e00\u4e2a\u4e09\u89d2\u5f62\u662f\u5426\u4e3a\u7b49\u8170\u4e09\u89d2\u5f62\uff1f<\/strong><br \/>\u8981\u5224\u65ad\u4e00\u4e2a\u4e09\u89d2\u5f62\u662f\u5426\u4e3a\u7b49\u8170\u4e09\u89d2\u5f62\uff0c\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u4e09\u6761\u8fb9\u7684\u957f\u5ea6\u3002\u4e00\u4e2a\u4e09\u89d2\u5f62\u662f\u7b49\u8170\u7684\uff0c\u5f53\u4e14\u4ec5\u5f53\u81f3\u5c11\u6709\u4e24\u6761\u8fb9\u7684\u957f\u5ea6\u76f8\u7b49\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6bd4\u5982\uff1a\u5982\u679c\u8fb9\u957fa\u4e0e\u8fb9\u957fb\u76f8\u7b49\uff0c\u6216\u8005\u8fb9\u957fb\u4e0e\u8fb9\u957fc\u76f8\u7b49\uff0c\u6216\u8005\u8fb9\u957fa\u4e0e\u8fb9\u957fc\u76f8\u7b49\uff0c\u90a3\u4e48\u8fd9\u4e2a\u4e09\u89d2\u5f62\u5c31\u662f\u7b49\u8170\u4e09\u89d2\u5f62\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8f93\u5165\u4e09\u89d2\u5f62\u7684\u8fb9\u957f\u5e76\u8fdb\u884c\u5224\u65ad\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u8fb9\u957f\u3002\u8f93\u5165\u540e\uff0c\u5c06\u8fd9\u4e9b\u503c\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u6216\u6574\u6570\uff0c\u5e76\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6bd4\u8f83\u8fd9\u4e9b\u8fb9\u957f\uff0c\u8f93\u51fa\u7ed3\u679c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">a = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))\nb = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))\nc = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e09\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))\n\nif a == b or b == c or a == c:\n    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u7b49\u8170\u4e09\u89d2\u5f62&quot;)\nelse:\n    print(&quot;\u8fd9\u4e0d\u662f\u4e00\u4e2a\u7b49\u8170\u4e09\u89d2\u5f62&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u65e0\u6548\u7684\u8f93\u5165\uff0c\u4f8b\u5982\u8d1f\u503c\u6216\u975e\u6570\u5b57\uff1f<\/strong><br \/>\u5904\u7406\u65e0\u6548\u8f93\u5165\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u8f93\u5165\u9519\u8bef\uff0c\u6216\u5728\u8f93\u5165\u540e\u68c0\u67e5\u8fb9\u957f\u662f\u5426\u4e3a\u6b63\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    a = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))\n    b = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))\n    c = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e09\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))\n    \n    if a &lt;= 0 or b &lt;= 0 or c &lt;= 0:\n        print(&quot;\u8fb9\u957f\u5fc5\u987b\u4e3a\u6b63\u6570&quot;)\n    elif a == b or b == c or a == c:\n        print(&quot;\u8fd9\u662f\u4e00\u4e2a\u7b49\u8170\u4e09\u89d2\u5f62&quot;)\n    else:\n        print(&quot;\u8fd9\u4e0d\u662f\u4e00\u4e2a\u7b49\u8170\u4e09\u89d2\u5f62&quot;)\nexcept ValueError:\n    print(&quot;\u8bf7\u8f93\u5165\u6709\u6548\u7684\u6570\u5b57&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u8fb9\u957f\u662f\u6709\u6548\u7684\uff0c\u5e76\u80fd\u6709\u6548\u5730\u5224\u65ad\u662f\u5426\u4e3a\u7b49\u8170\u4e09\u89d2\u5f62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u65ad\u7b49\u8170\u4e09\u89d2\u5f62\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5229\u7528\u4e24\u8fb9\u76f8\u7b49\u7684\u7279\u6027\u3001\u8ba1\u7b97\u6240\u6709\u8fb9\u957f\u5e76\u8fdb\u884c\u6bd4\u8f83\u3001\u4f7f\u7528math\u5e93\u8fdb\u884c\u7cbe\u786e\u8ba1\u7b97\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1141841,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141832"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1141832"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141832\/revisions"}],"predecessor-version":[{"id":1141843,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141832\/revisions\/1141843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141841"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}