{"id":1167602,"date":"2025-01-15T15:47:15","date_gmt":"2025-01-15T07:47:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167602.html"},"modified":"2025-01-15T15:47:18","modified_gmt":"2025-01-15T07:47:18","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%b7%b3%e8%bf%87input","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167602.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u8df3\u8fc7input"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25211441\/dd5cb5e6-95c9-4780-b1f1-d7243e0226c7.webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u8df3\u8fc7input\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8df3\u8fc7<code>input<\/code>\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<strong>\u9884\u8bbe\u9ed8\u8ba4\u503c\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u3001\u6a21\u62df\u8f93\u5165<\/strong>\u7b49\u3002\u8fd9\u91cc\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5373\u9884\u8bbe\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728\u8c03\u8bd5\u4ee3\u7801\u65f6\u907f\u514d\u624b\u52a8\u8f93\u5165\u6570\u636e\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e3a\u8f93\u5165\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u6765\u8df3\u8fc7<code>input<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad\u6765\u51b3\u5b9a\u662f\u4f7f\u7528\u9ed8\u8ba4\u503c\u8fd8\u662f\u7b49\u5f85\u7528\u6237\u8f93\u5165\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u6613\u4e8e\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_user_input(prompt, default_value=None, use_default=False):<\/p>\n<p>    if use_default:<\/p>\n<p>        print(f&quot;{prompt} (using default: {default_value})&quot;)<\/p>\n<p>        return default_value<\/p>\n<p>    else:<\/p>\n<p>        return input(prompt)<\/p>\n<p>name = get_user_input(&quot;Enter your name: &quot;, &quot;John Doe&quot;, use_default=True)<\/p>\n<p>print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u4f20\u9012<code>use_default=True<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u4f7f\u7528\u9ed8\u8ba4\u503c<code>&quot;John Doe&quot;<\/code>\uff0c\u4ece\u800c\u8df3\u8fc7\u5b9e\u9645\u7684\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u9884\u8bbe\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p><h2>1. \u4f7f\u7528\u6761\u4ef6\u5224\u65ad<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4e3a\u4e86\u65b9\u4fbf\u8c03\u8bd5\u548c\u6d4b\u8bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u6765\u51b3\u5b9a\u662f\u5426\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u5927\u91cf\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_user_input(prompt, default_value=None, use_default=False):<\/p>\n<p>    if use_default:<\/p>\n<p>        print(f&quot;{prompt} (using default: {default_value})&quot;)<\/p>\n<p>        return default_value<\/p>\n<p>    else:<\/p>\n<p>        return input(prompt)<\/p>\n<p>name = get_user_input(&quot;Enter your name: &quot;, &quot;John Doe&quot;, use_default=True)<\/p>\n<p>print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u4f20\u9012<code>use_default=True<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u4f7f\u7528\u9ed8\u8ba4\u503c<code>&quot;John Doe&quot;<\/code>\uff0c\u4ece\u800c\u8df3\u8fc7\u5b9e\u9645\u7684\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>2. \u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u9884\u8bbe\u9ed8\u8ba4\u503c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u4ece\u800c\u907f\u514d\u4fee\u6539\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_user_input(prompt, env_var=None, default_value=None):<\/p>\n<p>    value = os.getenv(env_var, default_value)<\/p>\n<p>    if value is not None:<\/p>\n<p>        print(f&quot;{prompt} (using environment variable: {value})&quot;)<\/p>\n<p>        return value<\/p>\n<p>    else:<\/p>\n<p>        return input(prompt)<\/p>\n<p>name = get_user_input(&quot;Enter your name: &quot;, &quot;USER_NAME&quot;, &quot;John Doe&quot;)<\/p>\n<p>print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u73af\u5883\u53d8\u91cf<code>USER_NAME<\/code>\u5b58\u5728\uff0c\u5219\u7a0b\u5e8f\u5c06\u4f7f\u7528\u5176\u503c\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p><h2>1. argparse\u6a21\u5757<\/h2>\n<\/p>\n<p><p>Python\u7684<code>argparse<\/code>\u6a21\u5757\u53ef\u4ee5\u8ba9\u6211\u4eec\u8f7b\u677e\u5730\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4ece\u800c\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002\u901a\u8fc7\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u63d0\u4f9b\u5fc5\u8981\u7684\u8f93\u5165\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    parser = argparse.ArgumentParser(description=&quot;Example program to demonstrate skipping input.&quot;)<\/p>\n<p>    parser.add_argument(&quot;--name&quot;, type=str, default=&quot;John Doe&quot;, help=&quot;Your name&quot;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    print(f&quot;Hello, {args.name}!&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570<code>--name<\/code>\u63d0\u4f9b\u8f93\u5165\u503c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u8fd0\u884c\u65f6\u7684\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>2. sys.argv<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528<code>sys.argv<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def main():<\/p>\n<p>    if len(sys.argv) &gt; 1:<\/p>\n<p>        name = sys.argv[1]<\/p>\n<p>    else:<\/p>\n<p>        name = input(&quot;Enter your name: &quot;)<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u547d\u4ee4\u884c\u53c2\u6570\u5b58\u5728\uff0c\u5219\u4f7f\u7528\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a\u8f93\u5165\u503c\uff0c\u5426\u5219\u8bf7\u6c42\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u6a21\u62df\u8f93\u5165<\/strong><\/h2>\n<p><h2>1. unittest\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>unittest<\/code>\u6a21\u5757\u4e2d\u7684<code>patch<\/code>\u65b9\u6cd5\u6765\u6a21\u62df\u7528\u6237\u8f93\u5165\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u6d4b\u8bd5\u9700\u8981\u7528\u6237\u8f93\u5165\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>from unittest.mock import patch<\/p>\n<p>def get_user_input(prompt):<\/p>\n<p>    return input(prompt)<\/p>\n<p>class TestUserInput(unittest.TestCase):<\/p>\n<p>    @patch(&#39;builtins.input&#39;, return_value=&#39;John Doe&#39;)<\/p>\n<p>    def test_get_user_input(self, mock_input):<\/p>\n<p>        self.assertEqual(get_user_input(&quot;Enter your name: &quot;), &quot;John Doe&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7<code>patch<\/code>\u65b9\u6cd5\u6a21\u62df\u4e86\u7528\u6237\u8f93\u5165<code>&quot;John Doe&quot;<\/code>\uff0c\u4ece\u800c\u8df3\u8fc7\u4e86\u5b9e\u9645\u7684\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>2. io.StringIO<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u6a21\u62df\u8f93\u5165\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>io.StringIO<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u6a21\u62df\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import io<\/p>\n<p>import sys<\/p>\n<p>def get_user_input(prompt):<\/p>\n<p>    return input(prompt)<\/p>\n<p>def main():<\/p>\n<p>    sys.stdin = io.StringIO(&quot;John Doe\\n&quot;)<\/p>\n<p>    name = get_user_input(&quot;Enter your name: &quot;)<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u5c06<code>sys.stdin<\/code>\u91cd\u5b9a\u5411\u5230<code>io.StringIO<\/code>\u5bf9\u8c61\uff0c\u5b9e\u73b0\u4e86\u6a21\u62df\u7528\u6237\u8f93\u5165<code>&quot;John Doe&quot;<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p><h2>1. JSON\u914d\u7f6e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u4e5f\u662f\u4e00\u79cd\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u8f93\u5165\u503c\u5b58\u50a8\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u8bfb\u53d6\u8fd9\u4e9b\u503c\uff0c\u4ece\u800c\u907f\u514d\u8fd0\u884c\u65f6\u7684\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def get_user_input(prompt, config, key):<\/p>\n<p>    if key in config:<\/p>\n<p>        print(f&quot;{prompt} (using config: {config[key]})&quot;)<\/p>\n<p>        return config[key]<\/p>\n<p>    else:<\/p>\n<p>        return input(prompt)<\/p>\n<p>def main():<\/p>\n<p>    with open(&#39;config.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>        config = json.load(file)<\/p>\n<p>    name = get_user_input(&quot;Enter your name: &quot;, config, &quot;name&quot;)<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6<code>config.json<\/code>\u5305\u542b\u7528\u6237\u8f93\u5165\u503c\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>2. INI\u914d\u7f6e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\u662fINI\u6587\u4ef6\u3002\u901a\u8fc7\u4f7f\u7528<code>configparser<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6INI\u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<p>def get_user_input(prompt, config, section, key):<\/p>\n<p>    if config.has_option(section, key):<\/p>\n<p>        value = config.get(section, key)<\/p>\n<p>        print(f&quot;{prompt} (using config: {value})&quot;)<\/p>\n<p>        return value<\/p>\n<p>    else:<\/p>\n<p>        return input(prompt)<\/p>\n<p>def main():<\/p>\n<p>    config = configparser.ConfigParser()<\/p>\n<p>    config.read(&#39;config.ini&#39;)<\/p>\n<p>    name = get_user_input(&quot;Enter your name: &quot;, config, &quot;DEFAULT&quot;, &quot;name&quot;)<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6<code>config.ini<\/code>\u5305\u542b\u7528\u6237\u8f93\u5165\u503c\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09<\/strong><\/h2>\n<p><h2>1. Tkinter<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\u6765\u4ee3\u66ff\u547d\u4ee4\u884c\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Tkinter<\/code>\u5e93\u3002\u901a\u8fc7\u521b\u5efa\u7b80\u5355\u7684GUI\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u8f93\u5165\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def get_user_input():<\/p>\n<p>    name = entry.get()<\/p>\n<p>    if not name:<\/p>\n<p>        name = &quot;John Doe&quot;<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>    root.destroy()<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;User Input&quot;)<\/p>\n<p>label = tk.Label(root, text=&quot;Enter your name:&quot;)<\/p>\n<p>label.pack()<\/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_user_input)<\/p>\n<p>button.pack()<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>Tkinter<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684GUI\u3002\u5982\u679c\u7528\u6237\u6ca1\u6709\u8f93\u5165\u6570\u636e\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c<code>&quot;John Doe&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h2>2. PyQt<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u6d41\u884c\u7684GUI\u5e93\u662f<code>PyQt<\/code>\u3002\u901a\u8fc7\u4f7f\u7528<code>PyQt<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u66f4\u590d\u6742\u548c\u7f8e\u89c2\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout<\/p>\n<p>class App(QWidget):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.initUI()<\/p>\n<p>    def initUI(self):<\/p>\n<p>        self.setWindowTitle(&#39;User Input&#39;)<\/p>\n<p>        layout = QVBoxLayout()<\/p>\n<p>        self.label = QLabel(&#39;Enter your name:&#39;, self)<\/p>\n<p>        layout.addWidget(self.label)<\/p>\n<p>        self.entry = QLineEdit(self)<\/p>\n<p>        layout.addWidget(self.entry)<\/p>\n<p>        self.button = QPushButton(&#39;Submit&#39;, self)<\/p>\n<p>        self.button.clicked.connect(self.get_user_input)<\/p>\n<p>        layout.addWidget(self.button)<\/p>\n<p>        self.setLayout(layout)<\/p>\n<p>        self.show()<\/p>\n<p>    def get_user_input(self):<\/p>\n<p>        name = self.entry.text()<\/p>\n<p>        if not name:<\/p>\n<p>            name = &quot;John Doe&quot;<\/p>\n<p>        print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>        self.close()<\/p>\n<p>def main():<\/p>\n<p>    app = QApplication(sys.argv)<\/p>\n<p>    ex = App()<\/p>\n<p>    sys.exit(app.exec_())<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>PyQt<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684GUI\u3002\u5982\u679c\u7528\u6237\u6ca1\u6709\u8f93\u5165\u6570\u636e\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c<code>&quot;John Doe&quot;<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u81ea\u52a8\u5316\u811a\u672c<\/strong><\/h2>\n<p><h2>1. Selenium<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5728Web\u73af\u5883\u4e2d\u81ea\u52a8\u5316\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Selenium<\/code>\u5e93\u3002\u901a\u8fc7\u7f16\u5199\u81ea\u52a8\u5316\u811a\u672c\uff0c\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u8f93\u5165\uff0c\u4ece\u800c\u907f\u514d\u624b\u52a8\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.keys import Keys<\/p>\n<p>def main():<\/p>\n<p>    driver = webdriver.Chrome()<\/p>\n<p>    driver.get(&quot;http:\/\/example.com\/form&quot;)<\/p>\n<p>    name_input = driver.find_element_by_name(&quot;name&quot;)<\/p>\n<p>    name_input.send_keys(&quot;John Doe&quot;)<\/p>\n<p>    name_input.send_keys(Keys.RETURN)<\/p>\n<p>    print(&quot;Form submitted with name: John Doe&quot;)<\/p>\n<p>    driver.quit()<\/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\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>Selenium<\/code>\u6a21\u62df\u4e86\u5728Web\u8868\u5355\u4e2d\u8f93\u5165<code>&quot;John Doe&quot;<\/code>\u5e76\u63d0\u4ea4\u8868\u5355\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>2. PyAutoGUI<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u81ea\u52a8\u5316\u5de5\u5177\u662f<code>PyAutoGUI<\/code>\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u684c\u9762\u81ea\u52a8\u5316\u3002\u901a\u8fc7\u7f16\u5199\u81ea\u52a8\u5316\u811a\u672c\uff0c\u53ef\u4ee5\u6a21\u62df\u952e\u76d8\u8f93\u5165\u548c\u9f20\u6807\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyautogui<\/p>\n<p>def main():<\/p>\n<p>    pyautogui.click(100, 100)  # \u70b9\u51fb\u8f93\u5165\u6846<\/p>\n<p>    pyautogui.typewrite(&quot;John Doe&quot;)<\/p>\n<p>    pyautogui.press(&quot;enter&quot;)<\/p>\n<p>    print(&quot;Input provided: John Doe&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>PyAutoGUI<\/code>\u6a21\u62df\u4e86\u5728\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8f93\u5165<code>&quot;John Doe&quot;<\/code>\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u7f51\u7edc\u8bf7\u6c42<\/strong><\/h2>\n<p><h2>1. HTTP\u8bf7\u6c42<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u8bf7\u6c42\u83b7\u53d6\u8f93\u5165\u6570\u636e\u3002\u901a\u8fc7\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u9ed8\u8ba4\u503c\uff0c\u4ece\u800c\u907f\u514d\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_user_input(prompt, url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        value = response.text<\/p>\n<p>        print(f&quot;{prompt} (using server response: {value})&quot;)<\/p>\n<p>        return value<\/p>\n<p>    else:<\/p>\n<p>        return input(prompt)<\/p>\n<p>def main():<\/p>\n<p>    name = get_user_input(&quot;Enter your name: &quot;, &quot;http:\/\/example.com\/api\/name&quot;)<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u53d1\u9001HTTP\u8bf7\u6c42\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u8f93\u5165\u503c\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>2. WebSocket<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u7f51\u7edc\u901a\u4fe1\u65b9\u5f0f\u662fWebSocket\u3002\u901a\u8fc7\u4f7f\u7528WebSocket\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u901a\u4fe1\uff0c\u4ece\u800c\u83b7\u53d6\u8f93\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import websocket<\/p>\n<p>def on_message(ws, message):<\/p>\n<p>    print(f&quot;Server message: {message}&quot;)<\/p>\n<p>    ws.close()<\/p>\n<p>def get_user_input(prompt, url):<\/p>\n<p>    ws = websocket.WebSocketApp(url, on_message=on_message)<\/p>\n<p>    ws.run_forever()<\/p>\n<p>def main():<\/p>\n<p>    get_user_input(&quot;Enter your name: &quot;, &quot;ws:\/\/example.com\/socket&quot;)<\/p>\n<p>    print(&quot;Input received from server&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7WebSocket\u4e0e\u670d\u52a1\u5668\u901a\u4fe1\uff0c\u4ece\u800c\u83b7\u53d6\u8f93\u5165\u6570\u636e\uff0c\u5b9e\u73b0\u4e86\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u6570\u636e\u5e93<\/strong><\/h2>\n<p><h2>1. SQLite<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u6570\u636e\u5e93\u5b58\u50a8\u548c\u68c0\u7d22\u8f93\u5165\u6570\u636e\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u9ed8\u8ba4\u503c\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u8bfb\u53d6\u8fd9\u4e9b\u503c\uff0c\u4ece\u800c\u907f\u514d\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def get_user_input(prompt, db_path, query):<\/p>\n<p>    conn = sqlite3.connect(db_path)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(query)<\/p>\n<p>    result = cursor.fetchone()<\/p>\n<p>    conn.close()<\/p>\n<p>    if result:<\/p>\n<p>        value = result[0]<\/p>\n<p>        print(f&quot;{prompt} (using database value: {value})&quot;)<\/p>\n<p>        return value<\/p>\n<p>    else:<\/p>\n<p>        return input(prompt)<\/p>\n<p>def main():<\/p>\n<p>    db_path = &#39;input.db&#39;<\/p>\n<p>    query = &#39;SELECT name FROM user_input LIMIT 1&#39;<\/p>\n<p>    name = get_user_input(&quot;Enter your name: &quot;, db_path, query)<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u67e5\u8be2SQLite\u6570\u636e\u5e93\u4e2d\u7684\u9ed8\u8ba4\u503c\uff0c\u5b9e\u73b0\u4e86\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>2. MySQL<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u5e93\u662fMySQL\u3002\u901a\u8fc7\u4f7f\u7528MySQL\uff0c\u6211\u4eec\u53ef\u4ee5\u5b58\u50a8\u548c\u68c0\u7d22\u66f4\u591a\u590d\u6742\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mysql.connector<\/p>\n<p>def get_user_input(prompt, db_config, query):<\/p>\n<p>    conn = mysql.connector.connect(db_config)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(query)<\/p>\n<p>    result = cursor.fetchone()<\/p>\n<p>    conn.close()<\/p>\n<p>    if result:<\/p>\n<p>        value = result[0]<\/p>\n<p>        print(f&quot;{prompt} (using database value: {value})&quot;)<\/p>\n<p>        return value<\/p>\n<p>    else:<\/p>\n<p>        return input(prompt)<\/p>\n<p>def main():<\/p>\n<p>    db_config = {<\/p>\n<p>        &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>        &#39;user&#39;: &#39;root&#39;,<\/p>\n<p>        &#39;password&#39;: &#39;password&#39;,<\/p>\n<p>        &#39;database&#39;: &#39;input_db&#39;<\/p>\n<p>    }<\/p>\n<p>    query = &#39;SELECT name FROM user_input LIMIT 1&#39;<\/p>\n<p>    name = get_user_input(&quot;Enter your name: &quot;, db_config, query)<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/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\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u67e5\u8be2MySQL\u6570\u636e\u5e93\u4e2d\u7684\u9ed8\u8ba4\u503c\uff0c\u5b9e\u73b0\u4e86\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u547d\u4ee4\u811a\u672c<\/strong><\/h2>\n<p><h2>1. Shell\u811a\u672c<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Shell\u811a\u672c\u6765\u63d0\u4f9b\u8f93\u5165\u503c\u3002\u901a\u8fc7\u7f16\u5199Shell\u811a\u672c\uff0c\u53ef\u4ee5\u5728\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">#!\/bin\/bash<\/p>\n<p>python3 -c &quot;import sys; print(&#39;Hello, &#39; + sys.argv[1] + &#39;!&#39;)&quot; &quot;John Doe&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7Shell\u811a\u672c\u4f20\u9012\u53c2\u6570\uff0c\u5b9e\u73b0\u4e86\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h2>2. Batch\u811a\u672c<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528Batch\u811a\u672c\u6765\u63d0\u4f9b\u8f93\u5165\u503c\u3002\u901a\u8fc7\u7f16\u5199Batch\u811a\u672c\uff0c\u53ef\u4ee5\u5728\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-batch\">@echo off<\/p>\n<p>python -c &quot;import sys; print(&#39;Hello, &#39; + sys.argv[1] + &#39;!&#39;)&quot; &quot;John Doe&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7Batch\u811a\u672c\u4f20\u9012\u53c2\u6570\uff0c\u5b9e\u73b0\u4e86\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u7efc\u5408\u5e94\u7528<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7efc\u5408\u4f7f\u7528\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u5b9e\u73b0\u7075\u6d3b\u7684\u8f93\u5165\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7ed3\u5408\u547d\u4ee4\u884c\u53c2\u6570\u3001\u914d\u7f6e\u6587\u4ef6\u548c\u6570\u636e\u5e93\uff0c\u63d0\u4f9b\u591a\u79cd\u8f93\u5165\u65b9\u5f0f\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>import json<\/p>\n<p>import sqlite3<\/p>\n<p>import os<\/p>\n<p>def get_user_input(prompt, args, config, db_path, query):<\/p>\n<p>    if args.name:<\/p>\n<p>        return args.name<\/p>\n<p>    elif config and &quot;name&quot; in config:<\/p>\n<p>        return config[&quot;name&quot;]<\/p>\n<p>    else:<\/p>\n<p>        conn = sqlite3.connect(db_path)<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(query)<\/p>\n<p>        result = cursor.fetchone()<\/p>\n<p>        conn.close()<\/p>\n<p>        if result:<\/p>\n<p>            return result[0]<\/p>\n<p>        else:<\/p>\n<p>            return input(prompt)<\/p>\n<p>def main():<\/p>\n<p>    parser = argparse.ArgumentParser(description=&quot;Example program to demonstrate skipping input.&quot;)<\/p>\n<p>    parser.add_argument(&quot;--name&quot;, type=str, help=&quot;Your name&quot;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    config_path = &#39;config.json&#39;<\/p>\n<p>    if os.path.exists(config_path):<\/p>\n<p>        with open(config_path, &#39;r&#39;) as file:<\/p>\n<p>            config = json.load(file)<\/p>\n<p>    else:<\/p>\n<p>        config = None<\/p>\n<p>    db_path = &#39;input.db&#39;<\/p>\n<p>    query = &#39;SELECT name FROM user_input LIMIT 1&#39;<\/p>\n<p>    name = get_user_input(&quot;Enter your name: &quot;, args, config, db_path, query)<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/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\u7efc\u5408\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u3001\u914d\u7f6e\u6587\u4ef6\u548c\u6570\u636e\u5e93\u591a\u79cd\u65b9\u5f0f\u63d0\u4f9b\u8f93\u5165\u503c\uff0c\u5b9e\u73b0\u4e86\u7075\u6d3b\u7684\u8f93\u5165\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a\u5728Python\u4e2d\u8df3\u8fc7<code>input<\/code>\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u9884\u8bbe\u9ed8\u8ba4\u503c\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u3001\u6a21\u62df\u8f93\u5165\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3001\u56fe\u5f62\u7528\u6237\u754c\u9762\u3001\u81ea\u52a8\u5316\u811a\u672c\u3001\u7f51\u7edc\u8bf7\u6c42\u3001\u6570\u636e\u5e93\u3001\u547d\u4ee4\u811a\u672c\u548c\u7efc\u5408\u5e94\u7528\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8df3\u8fc7\u7528\u6237\u8f93\u5165\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u662f\u5426\u9700\u8981\u8df3\u8fc7\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u7528\u6237\u4e0d\u8f93\u5165\u4efb\u4f55\u5185\u5bb9\uff0c\u7a0b\u5e8f\u5c06\u4f7f\u7528\u8fd9\u4e2a\u9ed8\u8ba4\u503c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u63d0\u524d\u8bbe\u7f6e\u8f93\u5165\u7684\u9ed8\u8ba4\u503c\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u6765\u5b58\u50a8\u9ed8\u8ba4\u503c\uff0c\u5e76\u5728\u7528\u6237\u672a\u8f93\u5165\u4efb\u4f55\u5185\u5bb9\u65f6\u4f7f\u7528\u8be5\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u5185\u5bb9\uff08\u76f4\u63a5\u56de\u8f66\u4f7f\u7528\u9ed8\u8ba4\u503c\uff09\uff1a&quot;) or &quot;\u9ed8\u8ba4\u503c&quot;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u76f4\u63a5\u6309\u4e0b\u56de\u8f66\uff0c<code>user_input<\/code>\u5c06\u88ab\u8d4b\u503c\u4e3a\u201c\u9ed8\u8ba4\u503c\u201d\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6765\u4ee3\u66ff\u8f93\u5165\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>sys.argv<\/code>\u6765\u4ece\u547d\u4ee4\u884c\u83b7\u53d6\u8f93\u5165\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u8df3\u8fc7<code>input()<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import sys\n\nif len(sys.argv) &gt; 1:\n    user_input = sys.argv[1]\nelse:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u5185\u5bb9\uff1a&quot;)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u7528\u6237\u5728\u547d\u4ee4\u884c\u4e2d\u63d0\u4f9b\u4e86\u53c2\u6570\uff0c\u7a0b\u5e8f\u5c06\u4f7f\u7528\u8be5\u53c2\u6570\u4f5c\u4e3a\u8f93\u5165\uff1b\u5426\u5219\uff0c\u5b83\u5c06\u63d0\u793a\u7528\u6237\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8df3\u8fc7input\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u9884\u8bbe\u9ed8\u8ba4\u503c\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u3001\u6a21\u62df\u8f93\u5165\u7b49\u3002\u8fd9\u91cc\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u5e38\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1167609,"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\/1167602"}],"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=1167602"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167602\/revisions"}],"predecessor-version":[{"id":1167613,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167602\/revisions\/1167613"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167609"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}