{"id":1142906,"date":"2025-01-08T22:44:56","date_gmt":"2025-01-08T14:44:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142906.html"},"modified":"2025-01-08T22:45:00","modified_gmt":"2025-01-08T14:45:00","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%88%a4%e6%96%ad%e4%b8%89%e8%a7%92%e5%bd%a2%e7%9a%84%e5%bd%a2%e7%8a%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1142906.html","title":{"rendered":"\u5982\u4f55\u7528python\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180808\/cf1420b7-ae77-437a-afef-ed37180d67f4.webp\" alt=\"\u5982\u4f55\u7528python\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6\" \/><\/p>\n<p><p> <strong>\u7528Python\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6<\/strong>\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u7ec3\u4e60\uff0c\u901a\u8fc7\u8f93\u5165\u4e09\u89d2\u5f62\u4e09\u8fb9\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u5224\u65ad\u5b83\u662f\u7b49\u8fb9\u4e09\u89d2\u5f62\u3001\u7b49\u8170\u4e09\u89d2\u5f62\u8fd8\u662f\u666e\u901a\u4e09\u89d2\u5f62\u3002<strong>\u8f93\u5165\u4e09\u8fb9\u957f\u5ea6\u3001\u5224\u65ad\u4e09\u89d2\u5f62\u7c7b\u578b\u3001\u7f16\u5199\u51fd\u6570<\/strong>\u662f\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8f93\u5165\u4e09\u8fb9\u957f\u5ea6<\/h3>\n<\/p>\n<p><p>\u5728\u5224\u65ad\u4e09\u89d2\u5f62\u7c7b\u578b\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u83b7\u53d6\u4e09\u89d2\u5f62\u7684\u4e09\u8fb9\u957f\u5ea6\u3002\u53ef\u4ee5\u901a\u8fc7\u7528\u6237\u8f93\u5165\u6765\u83b7\u53d6\u8fd9\u4e9b\u503c\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u53c2\u6570\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u4e09\u8fb9\u957f\u5ea6<\/p>\n<p>a = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))<\/p>\n<p>b = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))<\/p>\n<p>c = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e09\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5224\u65ad\u662f\u5426\u6784\u6210\u4e09\u89d2\u5f62<\/h3>\n<\/p>\n<p><p>\u5728\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6\u4e4b\u524d\uff0c\u5fc5\u987b\u5148\u786e\u5b9a\u8f93\u5165\u7684\u4e09\u8fb9\u957f\u5ea6\u80fd\u5426\u6784\u6210\u4e00\u4e2a\u4e09\u89d2\u5f62\u3002\u6839\u636e\u4e09\u89d2\u5f62\u4e0d\u7b49\u5f0f\u5b9a\u7406\uff0c\u4e09\u89d2\u5f62\u7684\u4efb\u610f\u4e24\u8fb9\u4e4b\u548c\u5fc5\u987b\u5927\u4e8e\u7b2c\u4e09\u8fb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_triangle(a, b, c):<\/p>\n<p>    return a + b &gt; c and a + c &gt; b and b + c &gt; a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u4e09\u8fb9\u957f\u5ea6\u7684\u5173\u7cfb\uff0c\u53ef\u4ee5\u5c06\u4e09\u89d2\u5f62\u5206\u4e3a\u7b49\u8fb9\u4e09\u89d2\u5f62\u3001\u7b49\u8170\u4e09\u89d2\u5f62\u548c\u666e\u901a\u4e09\u89d2\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def triangle_type(a, b, c):<\/p>\n<p>    if a == b == c:<\/p>\n<p>        return &quot;\u7b49\u8fb9\u4e09\u89d2\u5f62&quot;<\/p>\n<p>    elif a == b or a == c or b == c:<\/p>\n<p>        return &quot;\u7b49\u8170\u4e09\u89d2\u5f62&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;\u666e\u901a\u4e09\u89d2\u5f62&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u5199\u5b8c\u6574\u7684\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u51fd\u6570\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def determine_triangle(a, b, c):<\/p>\n<p>    if not is_triangle(a, b, c):<\/p>\n<p>        return &quot;\u8fd9\u4e0d\u662f\u4e00\u4e2a\u4e09\u89d2\u5f62&quot;<\/p>\n<p>    return triangle_type(a, b, c)<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u4e09\u8fb9\u957f\u5ea6<\/strong><\/h2>\n<p>a = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))<\/p>\n<p>b = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))<\/p>\n<p>c = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e09\u6761\u8fb9\u7684\u957f\u5ea6: &quot;))<\/p>\n<h2><strong>\u5224\u65ad\u4e09\u89d2\u5f62\u7c7b\u578b\u5e76\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>result = determine_triangle(a, b, c)<\/p>\n<p>print(&quot;\u8be5\u4e09\u89d2\u5f62\u662f:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6dfb\u52a0\u66f4\u591a\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u7a0b\u5e8f\u66f4\u5b8c\u5584\uff0c\u53ef\u4ee5\u8003\u8651\u6dfb\u52a0\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8f93\u5165\u9a8c\u8bc1<\/strong>\uff1a\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u6b63\u6570\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u975e\u6570\u503c\u60c5\u51b5\u3002<\/li>\n<li><strong>\u7528\u6237\u754c\u9762<\/strong>\uff1a\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u8f93\u5165\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u6b63\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_positive_number(prompt):<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            value = float(input(prompt))<\/p>\n<p>            if value &gt; 0:<\/p>\n<p>                return value<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;\u957f\u5ea6\u5fc5\u987b\u662f\u6b63\u6570\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)<\/p>\n<p>        except ValueError:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u503c\u3002&quot;)<\/p>\n<p>a = get_positive_number(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u6761\u8fb9\u7684\u957f\u5ea6: &quot;)<\/p>\n<p>b = get_positive_number(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u6761\u8fb9\u7684\u957f\u5ea6: &quot;)<\/p>\n<p>c = get_positive_number(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e09\u6761\u8fb9\u7684\u957f\u5ea6: &quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u975e\u6570\u503c\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_number(prompt):<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            value = float(input(prompt))<\/p>\n<p>            return value<\/p>\n<p>        except ValueError:<\/p>\n<p>            print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u503c\u3002&quot;)<\/p>\n<p>a = get_number(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u6761\u8fb9\u7684\u957f\u5ea6: &quot;)<\/p>\n<p>b = get_number(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u6761\u8fb9\u7684\u957f\u5ea6: &quot;)<\/p>\n<p>c = get_number(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e09\u6761\u8fb9\u7684\u957f\u5ea6: &quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u7528\u6237\u754c\u9762<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684GUI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import messagebox<\/p>\n<p>def determine_triangle_gui():<\/p>\n<p>    try:<\/p>\n<p>        a = float(entry_a.get())<\/p>\n<p>        b = float(entry_b.get())<\/p>\n<p>        c = float(entry_c.get())<\/p>\n<p>        if not is_triangle(a, b, c):<\/p>\n<p>            messagebox.showinfo(&quot;\u7ed3\u679c&quot;, &quot;\u8fd9\u4e0d\u662f\u4e00\u4e2a\u4e09\u89d2\u5f62&quot;)<\/p>\n<p>        else:<\/p>\n<p>            result = triangle_type(a, b, c)<\/p>\n<p>            messagebox.showinfo(&quot;\u7ed3\u679c&quot;, &quot;\u8be5\u4e09\u89d2\u5f62\u662f: &quot; + result)<\/p>\n<p>    except ValueError:<\/p>\n<p>        messagebox.showerror(&quot;\u8f93\u5165\u9519\u8bef&quot;, &quot;\u8bf7\u8f93\u5165\u6709\u6548\u7684\u6570\u503c&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u4e09\u89d2\u5f62\u7c7b\u578b\u5224\u65ad&quot;)<\/p>\n<p>tk.Label(root, text=&quot;\u7b2c\u4e00\u6761\u8fb9\u957f\u5ea6:&quot;).grid(row=0)<\/p>\n<p>tk.Label(root, text=&quot;\u7b2c\u4e8c\u6761\u8fb9\u957f\u5ea6:&quot;).grid(row=1)<\/p>\n<p>tk.Label(root, text=&quot;\u7b2c\u4e09\u6761\u8fb9\u957f\u5ea6:&quot;).grid(row=2)<\/p>\n<p>entry_a = tk.Entry(root)<\/p>\n<p>entry_b = tk.Entry(root)<\/p>\n<p>entry_c = tk.Entry(root)<\/p>\n<p>entry_a.grid(row=0, column=1)<\/p>\n<p>entry_b.grid(row=1, column=1)<\/p>\n<p>entry_c.grid(row=2, column=1)<\/p>\n<p>tk.Button(root, text=&quot;\u5224\u65ad&quot;, command=determine_triangle_gui).grid(row=3, column=0, columnspan=2)<\/p>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u7528Python\u7f16\u5199\u4e00\u4e2a\u529f\u80fd\u5168\u9762\u7684\u7a0b\u5e8f\u6765\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u7c7b\u578b\u3002\u4ece\u57fa\u672c\u7684\u63a7\u5236\u53f0\u8f93\u5165\u5230\u9ad8\u7ea7\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u5c55\u793a\u4e86\u5982\u4f55\u9010\u6b65\u5b8c\u5584\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6\uff1f<\/strong><br \/>\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6\u4e3b\u8981\u4f9d\u8d56\u4e8e\u4e09\u89d2\u5f62\u7684\u8fb9\u957f\u5173\u7cfb\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u8f93\u5165\u7684\u4e09\u6761\u8fb9\u957f\uff0c\u53ef\u4ee5\u5224\u65ad\u51fa\u8be5\u4e09\u89d2\u5f62\u662f\u7b49\u8fb9\u3001\u7b49\u8170\u8fd8\u662f\u4e0d\u7b49\u8fb9\u3002<\/p>\n<p><strong>\u9700\u8981\u4ec0\u4e48\u6837\u7684\u8f93\u5165\u624d\u80fd\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6\uff1f<\/strong><br \/>\u8981\u51c6\u786e\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6\uff0c\u7528\u6237\u9700\u8981\u63d0\u4f9b\u4e09\u6761\u8fb9\u7684\u957f\u5ea6\u3002\u8fd9\u4e09\u6761\u8fb9\u5fc5\u987b\u6ee1\u8db3\u4e09\u89d2\u5f62\u4e0d\u7b49\u5f0f\uff0c\u5373\u4efb\u610f\u4e24\u8fb9\u4e4b\u548c\u5927\u4e8e\u7b2c\u4e09\u8fb9\u3002\u8f93\u5165\u7684\u8fb9\u957f\u5fc5\u987b\u662f\u6b63\u6570\u3002<\/p>\n<p><strong>\u5982\u679c\u8f93\u5165\u7684\u8fb9\u957f\u65e0\u6cd5\u5f62\u6210\u4e09\u89d2\u5f62\uff0cPython\u4f1a\u5982\u4f55\u53cd\u9988\uff1f<\/strong><br \/>\u5728\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u52a0\u5165\u6761\u4ef6\u5224\u65ad\uff0c\u68c0\u67e5\u8f93\u5165\u7684\u8fb9\u957f\u662f\u5426\u7b26\u5408\u4e09\u89d2\u5f62\u4e0d\u7b49\u5f0f\u3002\u5982\u679c\u4e0d\u7b26\u5408\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u53cb\u597d\u7684\u63d0\u793a\u4fe1\u606f\uff0c\u4f8b\u5982\u201c\u63d0\u4f9b\u7684\u8fb9\u957f\u65e0\u6cd5\u7ec4\u6210\u4e09\u89d2\u5f62\uff0c\u8bf7\u68c0\u67e5\u8f93\u5165\u3002\u201d\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u4e86\u89e3\u95ee\u9898\u6240\u5728\u3002<\/p>\n<p><strong>\u5728\u5224\u65ad\u4e09\u89d2\u5f62\u5f62\u72b6\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5e38\u89c1\u9519\u8bef\u5305\u62ec\u8f93\u5165\u8d1f\u503c\u6216\u96f6\u3001\u672a\u80fd\u6b63\u786e\u5904\u7406\u8fb9\u957f\u76f8\u7b49\u7684\u60c5\u51b5\uff0c\u4ee5\u53ca\u6ca1\u6709\u8003\u8651\u6d6e\u70b9\u6570\u8f93\u5165\u7684\u8bef\u5dee\u3002\u786e\u4fdd\u6240\u6709\u8f93\u5165\u90fd\u4e3a\u6b63\u6570\uff0c\u5e76\u4f7f\u7528\u5408\u9002\u7684\u6bd4\u8f83\u65b9\u6cd5\u6765\u5224\u65ad\u8fb9\u957f\u7684\u5173\u7cfb\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5224\u65ad\u4e09\u89d2\u5f62\u7684\u5f62\u72b6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u7ec3\u4e60\uff0c\u901a\u8fc7\u8f93\u5165\u4e09\u89d2\u5f62\u4e09\u8fb9\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u5224\u65ad\u5b83\u662f\u7b49\u8fb9\u4e09\u89d2\u5f62\u3001\u7b49\u8170\u4e09\u89d2 [&hellip;]","protected":false},"author":3,"featured_media":1142920,"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\/1142906"}],"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=1142906"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142906\/revisions"}],"predecessor-version":[{"id":1142922,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142906\/revisions\/1142922"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142920"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}