{"id":1106776,"date":"2025-01-08T16:43:26","date_gmt":"2025-01-08T08:43:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1106776.html"},"modified":"2025-01-08T16:43:28","modified_gmt":"2025-01-08T08:43:28","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e5%8c%96%e5%ad%a6%e5%85%83%e7%b4%a0%e6%9f%a5%e8%af%a2%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1106776.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071115\/ed907b4a-faca-4d2f-81f2-dd8a86006054.webp\" alt=\"\u5982\u4f55\u7528python\u505a\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u505a\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u7a0b\u5e8f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u5178\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5143\u7d20\u4fe1\u606f\u3001\u5229\u7528\u7528\u6237\u8f93\u5165\u67e5\u8be2\u5143\u7d20\u4fe1\u606f\u3001\u901a\u8fc7\u51fd\u6570\u8fdb\u884c\u67e5\u8be2\u3002<\/strong> Python\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u9002\u5408\u5b58\u50a8\u548c\u5feb\u901f\u67e5\u627e\u6570\u636e\u7684\u7ed3\u6784\u3002\u4e3a\u4e86\u66f4\u8be6\u7ec6\u5730\u8bb2\u89e3\u5b9e\u73b0\u65b9\u6cd5\uff0c\u672c\u6587\u5c06\u4f1a\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5c55\u5f00\uff1a\u4e00\u3001\u51c6\u5907\u5143\u7d20\u6570\u636e\uff1b\u4e8c\u3001\u521b\u5efa\u67e5\u8be2\u51fd\u6570\uff1b\u4e09\u3001\u5b9e\u73b0\u7528\u6237\u4ea4\u4e92\uff1b\u56db\u3001\u4f18\u5316\u548c\u6269\u5c55\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u5143\u7d20\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u5316\u5b66\u5143\u7d20\u7684\u6570\u636e\u3002\u5316\u5b66\u5143\u7d20\u7684\u6570\u636e\u53ef\u4ee5\u5305\u62ec\u5143\u7d20\u7b26\u53f7\u3001\u5143\u7d20\u540d\u79f0\u3001\u539f\u5b50\u5e8f\u6570\u3001\u539f\u5b50\u8d28\u91cf\u7b49\u4fe1\u606f\u3002\u4e3a\u4e86\u7b80\u5316\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5b57\u5178\u6765\u5b58\u50a8\u8fd9\u4e9b\u4fe1\u606f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5316\u5b66\u5143\u7d20\u6570\u636e\u5b57\u5178\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = {<\/p>\n<p>    &quot;H&quot;: {&quot;name&quot;: &quot;Hydrogen&quot;, &quot;atomic_number&quot;: 1, &quot;atomic_mass&quot;: 1.008},<\/p>\n<p>    &quot;He&quot;: {&quot;name&quot;: &quot;Helium&quot;, &quot;atomic_number&quot;: 2, &quot;atomic_mass&quot;: 4.0026},<\/p>\n<p>    &quot;Li&quot;: {&quot;name&quot;: &quot;Lithium&quot;, &quot;atomic_number&quot;: 3, &quot;atomic_mass&quot;: 6.94},<\/p>\n<p>    &quot;Be&quot;: {&quot;name&quot;: &quot;Beryllium&quot;, &quot;atomic_number&quot;: 4, &quot;atomic_mass&quot;: 9.0122},<\/p>\n<p>    &quot;B&quot;: {&quot;name&quot;: &quot;Boron&quot;, &quot;atomic_number&quot;: 5, &quot;atomic_mass&quot;: 10.81},<\/p>\n<p>    # ... \u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u6240\u6709\u5143\u7d20<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u67e5\u8be2\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u67e5\u8be2\u5143\u7d20\u4fe1\u606f\u3002\u8fd9\u4e2a\u51fd\u6570\u5c06\u63a5\u53d7\u4e00\u4e2a\u5143\u7d20\u7b26\u53f7\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u5143\u7d20\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def query_element(symbol):<\/p>\n<p>    element = elements.get(symbol)<\/p>\n<p>    if element:<\/p>\n<p>        return f&quot;Element: {element[&#39;name&#39;]}\\nAtomic Number: {element[&#39;atomic_number&#39;]}\\nAtomic Mass: {element[&#39;atomic_mass&#39;]}&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;Element not found.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u7684 <code>get<\/code> \u65b9\u6cd5\u6765\u67e5\u627e\u5143\u7d20\u7b26\u53f7\u5bf9\u5e94\u7684\u5143\u7d20\u4fe1\u606f\u3002\u5982\u679c\u627e\u5230\u4e86\u5143\u7d20\uff0c\u5c31\u8fd4\u56de\u5143\u7d20\u7684\u8be6\u7ec6\u4fe1\u606f\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u201cElement not found\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u73b0\u7528\u6237\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u7528\u6237\u80fd\u591f\u901a\u8fc7\u8f93\u5165\u67e5\u8be2\u5143\u7d20\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u4ea4\u4e92\u754c\u9762\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u67e5\u8be2\u7a0b\u5e8f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    while True:<\/p>\n<p>        symbol = input(&quot;Enter element symbol (or &#39;exit&#39; to quit): &quot;).capitalize()<\/p>\n<p>        if symbol.lower() == &#39;exit&#39;:<\/p>\n<p>            break<\/p>\n<p>        print(query_element(symbol))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u4e0d\u65ad\u5730\u63d0\u793a\u7528\u6237\u8f93\u5165\u5143\u7d20\u7b26\u53f7\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u201cexit\u201d\u9000\u51fa\u7a0b\u5e8f\u3002\u6bcf\u6b21\u7528\u6237\u8f93\u5165\u5143\u7d20\u7b26\u53f7\u540e\uff0c\u7a0b\u5e8f\u4f1a\u8c03\u7528 <code>query_element<\/code> \u51fd\u6570\u5e76\u6253\u5370\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u548c\u6269\u5c55\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u4f18\u5316\u548c\u6269\u5c55\uff0c\u4ee5\u63d0\u4f9b\u66f4\u591a\u529f\u80fd\u548c\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u53161\uff1a\u63d0\u4f9b\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u67e5\u8be2<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u7528\u6237\u8f93\u5165\u7684\u5143\u7d20\u7b26\u53f7\u8f6c\u6362\u4e3a\u6807\u51c6\u683c\u5f0f\uff08\u9996\u5b57\u6bcd\u5927\u5199\uff09\u6765\u5b9e\u73b0\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">symbol = input(&quot;Enter element symbol (or &#39;exit&#39; to quit): &quot;).capitalize()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u53162\uff1a\u6dfb\u52a0\u66f4\u591a\u5143\u7d20\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u6269\u5c55\u5b57\u5178\uff0c\u4ee5\u5305\u62ec\u66f4\u591a\u5143\u7d20\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u7535\u5b50\u914d\u7f6e\u3001\u5143\u7d20\u7c7b\u522b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = {<\/p>\n<p>    &quot;H&quot;: {&quot;name&quot;: &quot;Hydrogen&quot;, &quot;atomic_number&quot;: 1, &quot;atomic_mass&quot;: 1.008, &quot;category&quot;: &quot;Nonmetal&quot;, &quot;electron_configuration&quot;: &quot;1s1&quot;},<\/p>\n<p>    &quot;He&quot;: {&quot;name&quot;: &quot;Helium&quot;, &quot;atomic_number&quot;: 2, &quot;atomic_mass&quot;: 4.0026, &quot;category&quot;: &quot;Noble gas&quot;, &quot;electron_configuration&quot;: &quot;1s2&quot;},<\/p>\n<p>    # ... \u5176\u4ed6\u5143\u7d20<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u53163\uff1a\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u7528\u6237\u754c\u9762<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982 <code>curses<\/code> \u6216 <code>tkinter<\/code> \u6765\u521b\u5efa\u66f4\u53cb\u597d\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <code>tkinter<\/code> \u7684\u793a\u4f8b\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 query_element_gui(symbol):<\/p>\n<p>    element = elements.get(symbol)<\/p>\n<p>    if element:<\/p>\n<p>        return f&quot;Element: {element[&#39;name&#39;]}\\nAtomic Number: {element[&#39;atomic_number&#39;]}\\nAtomic Mass: {element[&#39;atomic_mass&#39;]}\\nCategory: {element[&#39;category&#39;]}\\nElectron Configuration: {element[&#39;electron_configuration&#39;]}&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;Element not found.&quot;<\/p>\n<p>def on_search():<\/p>\n<p>    symbol = entry.get().capitalize()<\/p>\n<p>    result = query_element_gui(symbol)<\/p>\n<p>    messagebox.showinfo(&quot;Element Information&quot;, result)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Chemical Element Query&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u8f93\u5165\u6846\u548c\u6309\u94ae<\/strong><\/h2>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack(pady=10)<\/p>\n<p>button = tk.Button(root, text=&quot;Search&quot;, command=on_search)<\/p>\n<p>button.pack(pady=10)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2aGUI\u7a0b\u5e8f\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u56fe\u5f62\u754c\u9762\u8f93\u5165\u5143\u7d20\u7b26\u53f7\u5e76\u67e5\u8be2\u5143\u7d20\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u6574\u7684\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u7a0b\u5e8f\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u7a0b\u5e8f\uff0c\u4ee5\u63d0\u4f9b\u66f4\u591a\u529f\u80fd\u548c\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002\u5e0c\u671b\u8fd9\u4e2a\u6559\u7a0b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6765\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5229\u7528Python\u7684\u5b57\u5178\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u5143\u7d20\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u6bd4\u5982\u539f\u5b50\u5e8f\u6570\u3001\u7b26\u53f7\u3001\u540d\u79f0\u548c\u76f8\u5bf9\u539f\u5b50\u8d28\u91cf\u7b49\u3002\u901a\u8fc7\u7528\u6237\u8f93\u5165\u5143\u7d20\u540d\u79f0\u6216\u7b26\u53f7\uff0c\u7a0b\u5e8f\u80fd\u591f\u5feb\u901f\u68c0\u7d22\u5e76\u663e\u793a\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<p><strong>\u7a0b\u5e8f\u662f\u5426\u652f\u6301\u6839\u636e\u5143\u7d20\u7684\u6027\u8d28\u8fdb\u884c\u7b5b\u9009\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u6269\u5c55\u67e5\u8be2\u7a0b\u5e8f\u7684\u529f\u80fd\uff0c\u8ba9\u7528\u6237\u6839\u636e\u5143\u7d20\u7684\u6027\u8d28\uff08\u5982\u91d1\u5c5e\u3001\u975e\u91d1\u5c5e\u3001\u5364\u7d20\u7b49\uff09\u8fdb\u884c\u7b5b\u9009\u3002\u901a\u8fc7\u7ef4\u62a4\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u6027\u8d28\u7684\u5217\u8868\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u7279\u5b9a\u6027\u8d28\uff0c\u7a0b\u5e8f\u5c06\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u96c6\u6210\u5230\u7f51\u9875\u5e94\u7528\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u3002\u5c06Python\u7a0b\u5e8f\u4e0eFlask\u6216Django\u7b49\u6846\u67b6\u7ed3\u5408\uff0c\u80fd\u591f\u5c06\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u529f\u80fd\u96c6\u6210\u5230\u7f51\u9875\u5e94\u7528\u4e2d\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u7f51\u9875\u8f93\u5165\u5143\u7d20\u4fe1\u606f\uff0c\u540e\u53f0\u4f7f\u7528Python\u5904\u7406\u67e5\u8be2\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u5347\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u7a0b\u5e8f\u7684\u7528\u6237\u4f53\u9a8c\uff1f<\/strong><br \/>\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f8b\u5982\u6dfb\u52a0\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff0c\u4f7f\u7528Tkinter\u6216PyQt\u7b49\u5e93\u521b\u5efa\u66f4\u53cb\u597d\u7684\u754c\u9762\u3002\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u8865\u5168\u529f\u80fd\uff0c\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u627e\u5230\u6240\u9700\u5143\u7d20\uff0c\u540c\u65f6\u63d0\u4f9b\u8be6\u7ec6\u7684\u8bf4\u660e\u548c\u793a\u4f8b\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u5143\u7d20\u7684\u7279\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u505a\u5316\u5b66\u5143\u7d20\u67e5\u8be2\u7a0b\u5e8f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u5178\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5143\u7d20\u4fe1\u606f\u3001\u5229\u7528\u7528\u6237\u8f93\u5165\u67e5\u8be2\u5143\u7d20\u4fe1\u606f [&hellip;]","protected":false},"author":3,"featured_media":1106783,"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\/1106776"}],"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=1106776"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106776\/revisions"}],"predecessor-version":[{"id":1106786,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106776\/revisions\/1106786"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1106783"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1106776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1106776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1106776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}