{"id":1169811,"date":"2025-01-15T16:15:04","date_gmt":"2025-01-15T08:15:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169811.html"},"modified":"2025-01-15T16:15:08","modified_gmt":"2025-01-15T08:15:08","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e7%bb%99%e5%8f%98%e9%87%8f%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169811.html","title":{"rendered":"python\u5982\u4f55\u8f93\u5165\u7ed9\u53d8\u91cf\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070659\/3c9bde61-c5cc-4d67-9af5-5d906715d258.webp\" alt=\"python\u5982\u4f55\u8f93\u5165\u7ed9\u53d8\u91cf\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8f93\u5165\u7ed9\u53d8\u91cf\u8d4b\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528input()\u51fd\u6570\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528input()\u51fd\u6570\u3002<strong>input()\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u53d8\u91cf<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528input()\u51fd\u6570\u4ee5\u53ca\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u6765\u7ed9\u53d8\u91cf\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 input() \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cinput()\u51fd\u6570\u7528\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\u6570\u636e\u3002\u5b83\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u3002\u8f93\u5165\u7684\u6570\u636e\u9ed8\u8ba4\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = input(&quot;Enter your name: &quot;)<\/p>\n<p>print(&quot;Hello, &quot; + name + &quot;!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u63d0\u793a\u7528\u6237\u8f93\u5165\u540d\u5b57\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u540d\u5b57\u8d4b\u503c\u7ed9\u53d8\u91cf <code>name<\/code>\uff0c\u7136\u540e\u6253\u5370\u51fa\u4e00\u6761\u95ee\u5019\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u7531\u4e8einput()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u9700\u8981\u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\uff08\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\uff0c\u4f8b\u5982int()\u6216float()\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = int(input(&quot;Enter your age: &quot;))<\/p>\n<p>height = float(input(&quot;Enter your height in meters: &quot;))<\/p>\n<p>print(f&quot;You are {age} years old and {height} meters tall.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u5e74\u9f84\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u8eab\u9ad8\u88ab\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u5904\u7406\u5f02\u5e38\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u8f93\u5165\u975e\u6570\u5b57\u5b57\u7b26\u5bfc\u81f4\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    age = int(input(&quot;Enter your age: &quot;))<\/p>\n<p>    print(f&quot;You are {age} years old.&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;Invalid input. Please enter a valid number.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u7ed9\u53d8\u91cf\u8d4b\u503c\u7684\u65b9\u6cd5\u662f\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u5982open()\u3001read()\u3001readline()\u7b49\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;data.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u540d\u4e3a&quot;data.txt&quot;\u7684\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6240\u6709\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>content<\/code>\u3002\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528readline()\u6216readlines()\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;data.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528strip()\u65b9\u6cd5\u53bb\u9664\u6bcf\u884c\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u9700\u8981\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u53d6\u8f93\u5165\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u4e2d\u7684argv\u5217\u8868\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>if len(sys.argv) != 3:<\/p>\n<p>    print(&quot;Usage: python script.py &lt;name&gt; &lt;age&gt;&quot;)<\/p>\n<p>    sys.exit(1)<\/p>\n<p>name = sys.argv[1]<\/p>\n<p>age = int(sys.argv[2])<\/p>\n<p>print(f&quot;Hello, {name}! You are {age} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u53d6\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>name<\/code> \u548c <code>age<\/code>\u3002\u5982\u679c\u547d\u4ee4\u884c\u53c2\u6570\u7684\u6570\u91cf\u4e0d\u6b63\u786e\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa\u4f7f\u7528\u8bf4\u660e\u5e76\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u914d\u7f6e\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u5b58\u50a8\uff0c\u5e76\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u3002\u5e38\u89c1\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\u5305\u62ecINI\u3001YAML\u3001JSON\u7b49\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u89e3\u6790\u8fd9\u4e9b\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u4f7f\u7528 configparser \u8bfb\u53d6 INI \u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&quot;config.ini&quot;)<\/p>\n<p>username = config[&quot;DEFAULT&quot;][&quot;Username&quot;]<\/p>\n<p>password = config[&quot;DEFAULT&quot;][&quot;Password&quot;]<\/p>\n<p>print(f&quot;Username: {username}, Password: {password}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u540d\u4e3a&quot;config.ini&quot;\u7684INI\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>username<\/code> \u548c <code>password<\/code>\u3002<\/p>\n<\/p>\n<p><h4>4.2\u3001\u4f7f\u7528 json \u8bfb\u53d6 JSON \u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&quot;config.json&quot;, &quot;r&quot;) as file:<\/p>\n<p>    config = json.load(file)<\/p>\n<p>username = config[&quot;username&quot;]<\/p>\n<p>password = config[&quot;password&quot;]<\/p>\n<p>print(f&quot;Username: {username}, Password: {password}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u540d\u4e3a&quot;config.json&quot;\u7684JSON\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>username<\/code> \u548c <code>password<\/code>\u3002<\/p>\n<\/p>\n<p><h4>4.3\u3001\u4f7f\u7528 PyYAML \u8bfb\u53d6 YAML \u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<p>with open(&quot;config.yaml&quot;, &quot;r&quot;) as file:<\/p>\n<p>    config = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(file)<\/p>\n<p>username = config[&quot;username&quot;]<\/p>\n<p>password = config[&quot;password&quot;]<\/p>\n<p>print(f&quot;Username: {username}, Password: {password}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u540d\u4e3a&quot;config.yaml&quot;\u7684YAML\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>username<\/code> \u548c <code>password<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u9700\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u8fde\u63a5\u548c\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u5982sqlite3\u3001pymysql\u3001psycopg2\u7b49\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u4f7f\u7528 sqlite3 \u8bfb\u53d6 SQLite \u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&quot;example.db&quot;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&quot;SELECT username, password FROM users WHERE id = 1&quot;)<\/p>\n<p>row = cursor.fetchone()<\/p>\n<p>username = row[0]<\/p>\n<p>password = row[1]<\/p>\n<p>print(f&quot;Username: {username}, Password: {password}&quot;)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u540d\u4e3a&quot;example.db&quot;\u7684SQLite\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>username<\/code> \u548c <code>password<\/code>\u3002<\/p>\n<\/p>\n<p><h4>5.2\u3001\u4f7f\u7528 pymysql \u8bfb\u53d6 MySQL \u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>conn = pymysql.connect(host=&quot;localhost&quot;, user=&quot;user&quot;, password=&quot;password&quot;, database=&quot;dbname&quot;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&quot;SELECT username, password FROM users WHERE id = 1&quot;)<\/p>\n<p>row = cursor.fetchone()<\/p>\n<p>username = row[0]<\/p>\n<p>password = row[1]<\/p>\n<p>print(f&quot;Username: {username}, Password: {password}&quot;)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4eceMySQL\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>username<\/code> \u548c <code>password<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4eceAPI\u6216Web\u670d\u52a1\u4e2d\u83b7\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u4eceAPI\u6216Web\u670d\u52a1\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u5904\u7406\u54cd\u5e94\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&quot;https:\/\/api.example.com\/user\/1&quot;)<\/p>\n<p>data = response.json()<\/p>\n<p>username = data[&quot;username&quot;]<\/p>\n<p>password = data[&quot;password&quot;]<\/p>\n<p>print(f&quot;Username: {username}, Password: {password}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u4e00\u4e2aREST API\u4e2d\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>username<\/code> \u548c <code>password<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7279\u522b\u662f\u5728\u90e8\u7f72\u548c\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u4f20\u9012\u914d\u7f6e\u4fe1\u606f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u6765\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>username = os.getenv(&quot;USERNAME&quot;)<\/p>\n<p>password = os.getenv(&quot;PASSWORD&quot;)<\/p>\n<p>print(f&quot;Username: {username}, Password: {password}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u8bfb\u53d6\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>username<\/code> \u548c <code>password<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\uff0c\u4ee5\u83b7\u53d6\u8f93\u5165\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528argparse\u6a21\u5757\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u7ed3\u5408input()\u51fd\u6570\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&quot;User Information&quot;)<\/p>\n<p>parser.add_argument(&quot;--name&quot;, type=str, help=&quot;User&#39;s name&quot;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>if args.name:<\/p>\n<p>    print(f&quot;Hello, {args.name}!&quot;)<\/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><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u5148\u5c1d\u8bd5\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u83b7\u53d6\u7528\u6237\u7684\u540d\u5b57\uff0c\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\uff0c\u5219\u901a\u8fc7input()\u51fd\u6570\u63d0\u793a\u7528\u6237\u8f93\u5165\u540d\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528GUI\u754c\u9762\u83b7\u53d6\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u6765\u83b7\u53d6\u8f93\u5165\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528tkinter\u5e93\u6765\u521b\u5efa\u7b80\u5355\u7684GUI\u754c\u9762\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import simpledialog<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<p>username = simpledialog.askstring(&quot;Input&quot;, &quot;Enter your username:&quot;)<\/p>\n<p>password = simpledialog.askstring(&quot;Input&quot;, &quot;Enter your password:&quot;, show=&quot;*&quot;)<\/p>\n<p>print(f&quot;Username: {username}, Password: {password}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7a0b\u5e8f\u4f7f\u7528tkinter\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684GUI\u5bf9\u8bdd\u6846\uff0c\u63d0\u793a\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf <code>username<\/code> \u548c <code>password<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u6a21\u5757\u5316\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u8fd4\u56de\u503c\u6765\u7ed9\u53d8\u91cf\u8d4b\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_user_info():<\/p>\n<p>    name = input(&quot;Enter your name: &quot;)<\/p>\n<p>    age = int(input(&quot;Enter your age: &quot;))<\/p>\n<p>    return name, age<\/p>\n<p>name, age = get_user_info()<\/p>\n<p>print(f&quot;Hello, {name}! You are {age} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570get_user_info()\uff0c\u8be5\u51fd\u6570\u4ece\u7528\u6237\u83b7\u53d6\u540d\u5b57\u548c\u5e74\u9f84\uff0c\u5e76\u8fd4\u56de\u8fd9\u4e9b\u503c\u3002\u7136\u540e\u5c06\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>name<\/code> \u548c <code>age<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f93\u5165\u6570\u636e\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u3001\u4ece\u914d\u7f6e\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u3001\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u3001\u4eceAPI\u6216Web\u670d\u52a1\u4e2d\u83b7\u53d6\u6570\u636e\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u4ea4\u4e92\u3001\u4f7f\u7528GUI\u754c\u9762\u83b7\u53d6\u8f93\u5165\u4ee5\u53ca\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u7b49\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<p><p><strong>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u786e\u4fdd\u5904\u7406\u597d\u5f02\u5e38\u60c5\u51b5\u548c\u6570\u636e\u9a8c\u8bc1\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c<\/strong>\u3002\u901a\u8fc7\u5408\u7406\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u6a21\u5757\u5316\u3001\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u53d8\u91cf\u8d4b\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4e3a\u53d8\u91cf\u8d4b\u503c\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u4f7f\u7528\u7b49\u53f7\uff08=\uff09\u5c06\u4e00\u4e2a\u503c\u5206\u914d\u7ed9\u53d8\u91cf\u540d\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u5e76\u8d4b\u503c\uff1a<code>x = 10<\/code>\u3002\u8fd9\u5c06\u628a\u6574\u657010\u8d4b\u7ed9\u53d8\u91cfx\u3002Python\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u548c\u5e03\u5c14\u503c\u7b49\uff0c\u56e0\u6b64\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u4e3a\u53d8\u91cf\u8d4b\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<p><strong>Python\u53d8\u91cf\u540d\u6709\u4ec0\u4e48\u89c4\u5219\u548c\u9650\u5236\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u540d\u5fc5\u987b\u9075\u5faa\u4e00\u4e9b\u57fa\u672c\u89c4\u5219\u3002\u53d8\u91cf\u540d\u53ef\u4ee5\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u4f46\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002\u6b64\u5916\uff0c\u53d8\u91cf\u540d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u4f8b\u5982<code>myVar<\/code>\u548c<code>myvar<\/code>\u88ab\u89c6\u4e3a\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\u3002\u907f\u514d\u4f7f\u7528Python\u7684\u4fdd\u7559\u5b57\uff08\u5982<code>if<\/code>\u3001<code>for<\/code>\u3001<code>while<\/code>\u7b49\uff09\u4f5c\u4e3a\u53d8\u91cf\u540d\uff0c\u4ee5\u514d\u5f15\u8d77\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4e00\u6b21\u6027\u4e3a\u591a\u4e2a\u53d8\u91cf\u8d4b\u503c\uff1f<\/strong><br \/>Python\u5141\u8bb8\u540c\u65f6\u4e3a\u591a\u4e2a\u53d8\u91cf\u8d4b\u503c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a\u53d8\u91cf\uff0c\u5e76\u5c06\u5176\u4e0e\u4e00\u4e2a\u503c\u5143\u7ec4\u8fdb\u884c\u914d\u5bf9\u3002\u4f8b\u5982\uff0c<code>x, y, z = 1, 2, 3<\/code>\u5c061\u8d4b\u7ed9x\uff0c2\u8d4b\u7ed9y\uff0c3\u8d4b\u7ed9z\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u503c\u4e3a\u591a\u4e2a\u53d8\u91cf\u8d4b\u503c\uff0c\u5982<code>a = b = c = 100<\/code>\uff0c\u8fd9\u5c06\u628a100\u8d4b\u7ed9\u53d8\u91cfa\u3001b\u548cc\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8f93\u5165\u7ed9\u53d8\u91cf\u8d4b\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528input()\u51fd\u6570\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u7b49\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1169822,"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\/1169811"}],"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=1169811"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169811\/revisions"}],"predecessor-version":[{"id":1169823,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169811\/revisions\/1169823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169822"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}