{"id":1169112,"date":"2025-01-15T16:06:35","date_gmt":"2025-01-15T08:06:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169112.html"},"modified":"2025-01-15T16:06:37","modified_gmt":"2025-01-15T08:06:37","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169112.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u7528\u6237\u8f93\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070153\/ada87866-fc97-4017-a6b6-f628f97feca9.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u7528\u6237\u8f93\u5165\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u4e3b\u8981\u65b9\u6cd5\u662f\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u3001\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570 \u548c \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong>\u3002 <code>input()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u5b83\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5e76\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u5e93\u4e2d\u7684<code>sys.argv<\/code>\u6765\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>input()<\/code>\u51fd\u6570\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p><code>input()<\/code>\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u65b9\u6cd5\u3002\u5b83\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u8fd0\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u5e76\u5728\u7528\u6237\u6309\u4e0b\u56de\u8f66\u952e\u540e\u8fd4\u56de\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;Please enter something: &quot;)<\/p>\n<p>print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u6574\u6570\u548c\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>input()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5982\u679c\u9700\u8981\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>int()<\/code> \u6216 <code>float()<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = int(input(&quot;Please enter an integer: &quot;))<\/p>\n<p>print(f&quot;You entered integer: {num}&quot;)<\/p>\n<p>num_float = float(input(&quot;Please enter a float number: &quot;))<\/p>\n<p>print(f&quot;You entered float: {num_float}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u591a\u884c\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u8bfb\u53d6\u591a\u884c\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u53cd\u590d\u8c03\u7528<code>input()<\/code>\u51fd\u6570\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Enter multiple lines of text. Type &#39;END&#39; to finish.&quot;)<\/p>\n<p>lines = []<\/p>\n<p>while True:<\/p>\n<p>    line = input()<\/p>\n<p>    if line.strip().upper() == &#39;END&#39;:<\/p>\n<p>        break<\/p>\n<p>    lines.append(line)<\/p>\n<p>print(&quot;You entered:&quot;)<\/p>\n<p>print(&quot;\\n&quot;.join(lines))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4ea4\u4e92\u5f0f\u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff0cPython\u7a0b\u5e8f\u4e5f\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7528\u4e8e\u811a\u672c\u548c\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>sys.argv<\/code> \u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570<\/h4>\n<\/p>\n<p><p><code>sys.argv<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86\u547d\u4ee4\u884c\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u7684\u540d\u79f0\uff0c\u540e\u7eed\u5143\u7d20\u662f\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Script name:&quot;, sys.argv[0])<\/p>\n<p>for i, arg in enumerate(sys.argv[1:], start=1):<\/p>\n<p>    print(f&quot;Argument {i}: {arg}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>argparse<\/code> \u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&#39;Process some integers.&#39;)<\/p>\n<p>parser.add_argument(&#39;integers&#39;, metavar=&#39;N&#39;, type=int, nargs=&#39;+&#39;, help=&#39;an integer for the accumulator&#39;)<\/p>\n<p>parser.add_argument(&#39;--sum&#39;, dest=&#39;accumulate&#39;, action=&#39;store_const&#39;, const=sum, default=max, help=&#39;sum the integers (default: find the max)&#39;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>print(args.accumulate(args.integers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u7684\u7528\u6237\u8f93\u5165\uff0c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7528\u6237\u8f93\u5165\u65b9\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>open()<\/code>\u6765\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7d20\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>    for line in lines:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\u66f4\u4e3a\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u4e00\u6b21\u6027\u5c06\u6587\u4ef6\u7684\u6240\u6709\u5185\u5bb9\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u547d\u4ee4\u884c\u548c\u6587\u4ef6\u8bfb\u53d6\uff0cPython\u8fd8\u652f\u6301\u901a\u8fc7\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002\u5e38\u89c1\u7684GUI\u5e93\u5305\u62ec <code>tkinter<\/code>\u3001<code>PyQt<\/code> \u548c <code>wxPython<\/code>\u3002\u8fd9\u91cc\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 <code>tkinter<\/code> \u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u7b80\u5355\u7684 <code>tkinter<\/code> \u8f93\u5165\u6846<\/h4>\n<\/p>\n<p><p><code>tkinter<\/code> \u662fPython\u7684\u6807\u51c6GUI\u5e93\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6f14\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u8f93\u5165\u6846\u548c\u6309\u94ae\u6765\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def get_input():<\/p>\n<p>    user_input = entry.get()<\/p>\n<p>    print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;User Input Example&quot;)<\/p>\n<p>entry = tk.Entry(root)<\/p>\n<p>entry.pack()<\/p>\n<p>button = tk.Button(root, text=&quot;Submit&quot;, command=get_input)<\/p>\n<p>button.pack()<\/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><h4>2\u3001\u4f7f\u7528 <code>tkinter<\/code> \u521b\u5efa\u591a\u884c\u8f93\u5165\u6846<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5141\u8bb8\u7528\u6237\u8f93\u5165\u591a\u884c\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Text<\/code> \u5c0f\u90e8\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def get_input():<\/p>\n<p>    user_input = text.get(&quot;1.0&quot;, tk.END)<\/p>\n<p>    print(f&quot;You entered:\\n{user_input}&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Multi-line Input Example&quot;)<\/p>\n<p>text = tk.Text(root, height=10, width=40)<\/p>\n<p>text.pack()<\/p>\n<p>button = tk.Button(root, text=&quot;Submit&quot;, command=get_input)<\/p>\n<p>button.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7f51\u7edc\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u6784\u5efa\u4e00\u4e2aWeb\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Flask<\/code> \u6216 <code>Django<\/code> \u7b49Web\u6846\u67b6\u6765\u5904\u7406\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>Flask<\/code> \u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/h4>\n<\/p>\n<p><p><code>Flask<\/code> \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u7528\u4e8e\u6784\u5efa\u7b80\u5355\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 <code>Flask<\/code> \u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, render_template_string<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<\/p>\n<p>def index():<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        user_input = request.form[&#39;user_input&#39;]<\/p>\n<p>        return f&quot;You entered: {user_input}&quot;<\/p>\n<p>    return render_template_string(&#39;&#39;&#39;<\/p>\n<p>        &lt;form method=&quot;post&quot;&gt;<\/p>\n<p>            &lt;input type=&quot;text&quot; name=&quot;user_input&quot;&gt;<\/p>\n<p>            &lt;input type=&quot;submit&quot;&gt;<\/p>\n<p>        &lt;\/form&gt;<\/p>\n<p>    &#39;&#39;&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>Django<\/code> \u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/h4>\n<\/p>\n<p><p><code>Django<\/code> \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u7528\u4e8e\u6784\u5efa\u590d\u6742\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 <code>Django<\/code> \u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 <code>Django<\/code> \u9879\u76ee\u548c\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">django-admin startproject myproject<\/p>\n<p>cd myproject<\/p>\n<p>django-admin startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728 <code>myapp<\/code> \u7684 <code>views.py<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.shortcuts import render<\/p>\n<p>from django.http import HttpResponse<\/p>\n<p>def index(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        user_input = request.POST[&#39;user_input&#39;]<\/p>\n<p>        return HttpResponse(f&quot;You entered: {user_input}&quot;)<\/p>\n<p>    return render(request, &#39;index.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728 <code>myapp<\/code> \u7684 <code>urls.py<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.urls import path<\/p>\n<p>from . import views<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;&#39;, views.index, name=&#39;index&#39;),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5728 <code>myapp<\/code> \u7684 <code>templates<\/code> \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a <code>index.html<\/code> \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;User Input Example&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;form method=&quot;post&quot;&gt;<\/p>\n<p>        {% csrf_token %}<\/p>\n<p>        &lt;input type=&quot;text&quot; name=&quot;user_input&quot;&gt;<\/p>\n<p>        &lt;input type=&quot;submit&quot;&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c <code>Django<\/code> \u5f00\u53d1\u670d\u52a1\u5668\uff0c\u5e76\u8bbf\u95ee <code>http:\/\/127.0.0.1:8000\/<\/code>\uff0c\u5373\u53ef\u770b\u5230\u8f93\u5165\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u548c\u5de5\u5177\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u548c\u5de5\u5177\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>click<\/code> \u5e93\u6784\u5efa\u547d\u4ee4\u884c\u754c\u9762\uff0c\u6216\u8005\u4f7f\u7528 <code>prompt_toolkit<\/code> \u5e93\u521b\u5efa\u66f4\u590d\u6742\u7684\u4ea4\u4e92\u5f0f\u8f93\u5165\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>click<\/code> \u5e93<\/h4>\n<\/p>\n<p><p><code>click<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u547d\u4ee4\u884c\u754c\u9762\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u53cb\u597d\u7684API\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 <code>click<\/code> \u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import click<\/p>\n<p>@click.command()<\/p>\n<p>@click.option(&#39;--name&#39;, prompt=&#39;Your name&#39;, help=&#39;The person to greet.&#39;)<\/p>\n<p>def hello(name):<\/p>\n<p>    click.echo(f&quot;Hello {name}!&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>prompt_toolkit<\/code> \u5e93<\/h4>\n<\/p>\n<p><p><code>prompt_toolkit<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u5e94\u7528\u7a0b\u5e8f\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u652f\u6301\u8bed\u6cd5\u9ad8\u4eae\u3001\u81ea\u52a8\u8865\u5168\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 <code>prompt_toolkit<\/code> \u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prompt_toolkit import prompt<\/p>\n<p>user_input = prompt(&quot;Please enter something: &quot;)<\/p>\n<p>print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5305\u62ec\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u3001\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u901a\u8fc7\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3001\u4ee5\u53ca\u901a\u8fc7\u7f51\u7edc\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684\u4ea4\u4e92\u5f0f\u8f93\u5165\u3002<\/li>\n<li><strong>\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u811a\u672c\u548c\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/li>\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u83b7\u53d6\u8f93\u5165\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/li>\n<li><strong>\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u56fe\u5f62\u5316\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<li><strong>\u901a\u8fc7\u7f51\u7edc\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u9002\u7528\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5176\u4ed6\u5e93\u548c\u5de5\u5177\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u5982 <code>click<\/code> \u548c <code>prompt_toolkit<\/code>\uff0c\u9002\u7528\u4e8e\u6784\u5efa\u66f4\u590d\u6742\u7684\u547d\u4ee4\u884c\u754c\u9762\u548c\u4ea4\u4e92\u5f0f\u8f93\u5165\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u7528\u6237\u8f93\u5165\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u5e94\u7528\u9700\u6c42\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u4f1a\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u6700\u4f73\u7684\u7528\u6237\u4f53\u9a8c\u548c\u529f\u80fd\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002\u8be5\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u3002\u4f8b\u5982\uff0c<code>user_input = input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u6570\u636e\uff1a&quot;)<\/code>\u3002\u60a8\u53ef\u4ee5\u5728\u63d0\u793a\u4fe1\u606f\u4e2d\u6307\u5b9a\u8981\u663e\u793a\u7684\u6587\u672c\uff0c\u4ee5\u5f15\u5bfc\u7528\u6237\u8f93\u5165\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u901a\u8fc7\u54ea\u4e9b\u65b9\u5f0f\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff1f<\/strong><br \/>\u83b7\u53d6\u7528\u6237\u8f93\u5165\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u6216<code>float()<\/code>\u7b49\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7b26\u5408\u9884\u671f\u7684\u7c7b\u578b\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u8f93\u5165\u7684\u6709\u6548\u6027\uff0c\u4f8b\u5982\uff0c\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u6570\u5b57\u800c\u4e0d\u662f\u5b57\u6bcd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u6349\u548c\u5904\u7406\u7528\u6237\u8f93\u5165\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4e0d\u80fd\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\uff0c\u53ef\u4ee5\u6355\u83b7<code>ValueError<\/code>\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u907f\u514d\u7a0b\u5e8f\u56e0\u9519\u8bef\u8f93\u5165\u800c\u5d29\u6e83\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    user_input = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff1a&quot;))\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u7528\u6237\u8f93\u5165\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u4e3b\u8981\u65b9\u6cd5\u662f\u4f7f\u7528 input() \u51fd\u6570\u3001\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570 \u548c \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002 input [&hellip;]","protected":false},"author":3,"featured_media":1169117,"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\/1169112"}],"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=1169112"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169112\/revisions"}],"predecessor-version":[{"id":1169119,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169112\/revisions\/1169119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169117"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}