{"id":1029,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u6761\u4ef6\u63a7\u5236","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1029.html","title":{"rendered":"Python \u6761\u4ef6\u63a7\u5236"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python \u6761\u4ef6\u63a7\u5236<\/h1>\n

\u641e\u61c2Python\u7a0b\u5e8f\u6267\u884c\u4e2d\u7684\u6761\u4ef6\u63a7\u5236<\/span>\n <\/div>\n

\n Python\u7a0b\u5e8f\u6267\u884c\u987a\u5e8f\u662f\u4ece\u5934\u5230\u5c3e\u7684\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\uff0c\u6240\u4ee5\u6267\u884c\u8fc7\u7a0b\u4e2d\u9700\u8981\u901a\u8fc7\u6761\u4ef6\u63a7\u5236\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002\u4f7f\u7528 if\u3001if else\u6216if elif else \u8bed\u53e5\u5bf9\u6761\u4ef6\u8fdb\u884c\u5224\u65ad\uff0c\u53ef\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\uff0c\u5b83\u4eec\u7684\u8bed\u6cd5\u548c\u6267\u884c\u6d41\u7a0b\u5982\u4e0b\u56fe\u6240\u793a\u3002\n <\/div>\n

Python \u6761\u4ef6\u63a7\u5236_https:\/\/bianchenghao6.com_\u3010Python3 \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\uff1a\n <\/div>\n

Python \u6761\u4ef6\u63a7\u5236_https:\/\/bianchenghao6.com_\u3010Python3 \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\u7ed3\u6784<\/h2>\n
\n Python\u4e2dif\u8bed\u53e5\u7684\u4e00\u822c\u5f62\u5f0f\u5982\u4e0b\u6240\u793a\uff1a\n <\/div>\n
\n
 if<\/span> condition_1:<\/span>
    statement_block_1<\/span>
elif<\/span> condition_2:<\/span>
    statement_block_2<\/span>
else:<\/span>
    statement_block_3<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u82e5 condition_1 \u6761\u4ef6\u6210\u7acb\uff08True\uff09\uff0c\u5c06\u6267\u884c statement_block_1 \u4e1a\u52a1\u4ee3\u7801\u5757<\/span>
\n \u82e5 condition_1 \u6761\u4ef6\u4e0d\u6210\u7acb\uff08False\uff09\uff0c\u5c06\u8df3\u8fc7 statement_block_1 \u4e1a\u52a1\u4ee3\u7801\u5757\uff0c\u8fdb\u5165\u4e0b\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad<\/span>
\n \u82e5 condition_2 \u6761\u4ef6\u6210\u7acb\uff08True\uff09\uff0c\u5c06\u6267\u884c statement_block_2 \u4e1a\u52a1\u4ee3\u7801\u5757<\/span>
\n \u82e5 condition_2 \u6761\u4ef6\u4e0d\u6210\u7acb\uff08False\uff09\uff0c\u5c06 statement_block_2 \u4e1a\u52a1\u4ee3\u7801\u5757\uff0c\u8fdb\u5165\u4e0b\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad<\/span> <\/p>\n

\n Python \u4e2d\u7528
\n elif<\/b>\u662f
\n else if<\/b>\u7684\u7b80\u5199\uff0c\u6240\u4ee5if\u8bed\u53e5\u7684\u5173\u952e\u5b57\u683c\u5f0f\u4e3a\uff1a
\n if \u2013 elif \u2013 else<\/b>\u3002\n <\/div>\n

Python \u6761\u4ef6\u63a7\u5236_https:\/\/bianchenghao6.com_\u3010Python3 \u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n \u6ce8\u610f\uff1a<\/span>\u6bcf\u4e2a\u6761\u4ef6\u540e\u9762\u8981\u4f7f\u7528\u5192\u53f7(:)\uff0c\u4f7f\u7528\u7f29\u8fdb\u6765\u5212\u5206\u8bed\u53e5\u5757\uff0c\u76f8\u540c\u7f29\u8fdb\u6570\u7684\u8bed\u53e5\u5728\u4e00\u8d77\u7ec4\u6210\u4e00\u4e2a\u8bed\u53e5\u5757\u3002\n <\/div>\n

\u5b9e\u4f8b<\/h2>\n
\n \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 if \u5b9e\u4f8b\uff1a\n <\/div>\n
\n
 age<\/span> = 20<\/span>
if<\/span> age >= 18:<\/span>
    print(\"\u606d\u559c\u4f60\uff0c\u4f60\u5df2\u7ecf\u6210\u5e74\u5566\uff5e\"<\/span>)
elif<\/span> age < 18 and age >12:<\/span>
    print(\"\u606d\u559c\u4f60\uff0c\u4f60\u5df2\u7ecf\u6210\u5e74\u5566\uff5e\"<\/span>)
elif<\/span> age < 13 and age >7:<\/span>
    print(\"\u4f60\u662f\u4e3a\u5c11\u513f\uff5e\"<\/span>)
else:<\/span>
    print(\"\u4f60\u662f\u4e3a\u5a74\u5e7c\u513f\uff5e\"<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
\u606d\u559c\u4f60\uff0c\u4f60\u5df2\u7ecf\u6210\u5e74\u5566\uff5e<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0b\u4e3aif\u4e2d\u5e38\u7528\u7684\u64cd\u4f5c\u8fd0\u7b97\u7b26:\n <\/div>\n\n\n\n\n\n\n\n\n\n
\u64cd\u4f5c\u7b26<\/td>\n\u63cf\u8ff0<\/td>\n<\/tr>\n
<<\/td>\n\u5c0f\u4e8e<\/td>\n<\/tr>\n
<= <\/td>\n\u5c0f\u4e8e\u6216\u7b49\u4e8e<\/td>\n<\/tr>\n
><\/td>\n\u5927\u4e8e<\/td>\n<\/tr>\n
>=<\/td>\n\u5927\u4e8e\u6216\u7b49\u4e8e<\/td>\n<\/tr>\n
==<\/td>\n\u7b49\u4e8e\uff0c\u6bd4\u8f83\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49<\/td>\n<\/tr>\n
!=<\/td>\n\u4e0d\u7b49\u4e8e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

if \u5d4c\u5957<\/h2>\n
\n Python\u4e2dif\u8bed\u53e5\u7684\u4e00\u822c\u5f62\u5f0f\u5982\u4e0b\u6240\u793a\uff1a\n <\/div>\n
\n
 if<\/span> condition_1:<\/span>
    statement_block_1<\/span>
    if<\/span> condition_2:<\/span>
        statement_block_3<\/span>
    elif<\/span> condition_3:<\/span>
        statement_block_3<\/span>
    else:<\/span>
        statement_block_4<\/span>
elif<\/span> condition_5:<\/span>
    statement_block_6<\/span>
else:<\/span>
    statement_block_6<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u4f8b\uff1a\n <\/div>\n
\n
 age<\/span> = 50<\/span>
if<\/span> age >= 18:<\/span>
    print(\"\u606d\u559c\u4f60\uff0c\u4f60\u5df2\u7ecf\u6210\u5e74\u5566\uff5e\"<\/span>)
    if<\/span> age == 50:<\/span>
        print(\"\u4f60\u7684\u5e74\u9f84\u662f50\u5c81\"<\/span>)
    elif<\/span> age == 40:<\/span>
        print(\"\u4f60\u7684\u5e74\u9f84\u662f40\u5c81\"<\/span>)
    else:<\/span>
        print(\"\u4f60\u7684\u5e74\u9f84\u4e0d\u662f30\u5c81\u4e5f\u4e0d\u662f40\u5c81\"<\/span>)
elif<\/span> age < 18 and age >12:<\/span>
    print(\"\u4f60\u7684\u5e74\u9f84\u572812\u5c81\u523018\u5c81\u4e4b\u95f4\"<\/span>)
else:<\/span>
    print(\"\u4f60\u7684\u5e74\u9f84\u5c0f\u4e8e12\u5c81\"<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
\u606d\u559c\u4f60\uff0c\u4f60\u5df2\u7ecf\u6210\u5e74\u5566\uff5e
\u4f60\u7684\u5e74\u9f84\u662f50\u5c81<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u6761\u4ef6\u63a7\u5236zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1029","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1029"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1029"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1029\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}