{"id":148644,"date":"2019-07-02T08:09:28","date_gmt":"2019-07-02T00:09:28","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=148644"},"modified":"2019-06-21T09:11:11","modified_gmt":"2019-06-21T01:11:11","slug":"python-function-data","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-function-data.html","title":{"rendered":"python\u51fd\u6570"},"content":{"rendered":"
\u201c\u51fd\u6570\u201d\u5c31\u50cf\u4e00\u4e2a\u7a0b\u5e8f\u5185\u7684\u5c0f\u7a0b\u5e8f<\/p>\n
\u5982\u679c\u8c03\u7528print()\u6216\u8005len()\u51fd\u6570\uff0c\u4f60\u4f1a\u4f20\u5165\u4e00\u4e9b\u503c\uff0c\u653e\u5728\u62ec\u53f7\u4e4b\u95f4\uff0c\u5728\u8fd9\u91cc\u79f0\u4e3a\u201c\u53c2\u6570\u201d\u3002\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49\u63a5\u6536\u53c2\u6570\u7684\u51fd\u6570\u3002<\/p>\n
>><\/span>> def<\/span> hello<\/span> (name)<\/span><\/span>:\r\n\tprint('hello '<\/span>+ name)\r\n>><\/span>> hello('alice'<\/span>)\r\nhello alice\r\n>><\/span>> hello('bob'<\/span>)\r\nhello bob\r\n<\/code><\/pre>\n\u5728\u8fd9\u4e2a\u7a0b\u5e8f\u7684hello\uff08\uff09\u51fd\u6570\u5b9a\u4e49\u4e2d\uff0c\u6709\u4e00\u4e2a\u540d\u4e3aname\u7684\u53d8\u5143\u3002\u201c\u53d8\u5143\u201d\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u53c2\u6570\u5c31\u5b58\u653e\u5728\u5176\u4e2d\u3002<\/p>\n
\u3010\u6ce8\u3011\u4fdd\u5b58\u5728\u53d8\u5143\u4e2d\u7684\u503c\uff0c\u5728\u51fd\u6570\u8fd4\u56de\u540e\u5c31\u4e22\u5931\u4e86\u3002<\/p>\n
3.2 \u8fd4\u56de\u503c\u548creturn\u8bed\u53e5<\/strong><\/strong><\/span><\/div>\n\u51fd\u6570\u8c03\u7528\u6c42\u503c\u7684\u7ed3\u679c\uff0c\u79f0\u4e3a\u51fd\u6570\u7684\u201c\u8fd4\u56de\u503c\u201d\u3002<\/p>\n
\u7528def\u8bed\u53e5\u7a7f\u521b\u5efa\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u7528return\u8bed\u53e5\u6307\u5b9a\u5e94\u8be5\u8fd4\u56de\u4ec0\u4e48\u503c\u3002return\u8bed\u53e5\u4e5f\u5305\u542b\u4ee5\u4e0b\u90e8\u5206<\/p>\n
\n- return \u5173\u952e\u5b57<\/li>\n
- \u51fd\u6570\u5e94\u8be5\u8fd4\u56de\u7684\u503c\u6216\u8868\u8fbe\u5f0f<\/li>\n<\/ul>\n
\u5982\u679c\u5728return\u8bed\u53e5\u4e2d\u4f7f\u7528\u4e86\u8868\u8fbe\u5f0f\uff0c\u8fd4\u56de\u503c\u5c31\u662f\u8be5\u8868\u8fbe\u5f0f\u6c42\u503c\u7684\u7ed3\u679c\u3002<\/p>\n
3.3 None\u503c<\/strong><\/strong><\/span><\/div>\n\u5728python\u4e2d\u6709\u4e00\u4e2a\u503c\u79f0\u4e3aNone\uff0c\u4ed6\u8868\u793a\u6ca1\u6709\u503c\u3002None\u662fNoneType\u6570\u636e\u7c7b\u578b\u7684\u552f\u4e00\u503c\u3002
\nprint()\u51fd\u6570\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u6587\u672c\uff0c\u4f46\u4ed6\u4e0d\u9700\u8981\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u8fd9\u5c31\u548clen()\u548cinput()\u4e0d\u540c\u3002\u4f46\u65e2\u7136\u6240\u6709\u51fd\u6570\u8c03\u7528\u90fd\u9700\u8981\u6c42\u503c\u4e3a\u4e00\u4e2a\u8fd4\u56de\u503c\uff0c\u90a3\u4e48print\uff08\uff09\u5c31\u8fd4\u56deNone<\/p>\n
>><\/span>> spam = print('hello'<\/span>)\r\nhello\r\n>><\/span>> None ==spam\r\nTrue\r\n>><\/span>> \r\n<\/code><\/pre>\n\u5728\u5e55\u540e\uff0c\u5bf9\u4e8e\u6ca1\u6709return\u8bed\u53e5\u7684\u51fd\u6570\u5b9a\u4e49\uff0cpython\u90fd\u4f1a\u5728\u672b\u5c3e\u52a0\u4e0areturn None\u3002\u8fd9\u7c7b\u4f3c\u4e8ewhile\u6216\u8005fo\u5faa\u73af\u9690\u5f0f\u7684\u4ee5continue\u8bed\u53e5\u7ed3\u5c3e\u3002\u800c\u4e14\uff0c\u5982\u679c\u4f7f\u7528\u5e26\u503c\u7684return\u8bed\u53e5\uff08\u4e5f\u5c31\u662f\u53ea\u6709return\u5173\u952e\u5b57\u672c\u8eab\uff09\uff0c\u90a3\u4e48\u5c31\u8fd4\u56deNone\u3002<\/p>\n
3.4 \u5173\u952e\u5b57\u53c2\u6570\u548cprint()<\/strong><\/strong><\/span><\/div>\n\u5927\u591a\u6570\u53c2\u6570\u662f\u7531\u4ed6\u4eec\u5728\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u4f4d\u7f6e\u6765\u8bc6\u522b\u7684\u3002\u4f46\u662f\u201c\u5173\u952e\u5b57\u53c2\u6570\u201d\u662f\u6709\u51fd\u6570\u8c03\u7528\u662f\u52a0\u5728\u4ed6\u4eec\u524d\u9762\u7684\u5173\u952e\u5b57\u6765\u8bc6\u522b\u7684\u3002\u5173\u952e\u5b57\u53c2\u6570\u901a\u5e38\u7528\u4e8e\u53ef\u9009\u53d8\u5143*\u3002\u4f8b\u5982\uff0cprint\uff08\uff09\u51fd\u6570\u6709\u53ef\u9009\u7684\u53d8\u5143end\u548csep\uff0c\u5206\u522b\u6307\u5b9a\u5728\u53c2\u6570\u672b\u5c3e\u6253\u5370\u4ec0\u4e48\uff0c\u4ee5\u53ca\u5728\u53c2\u6570\u4e4b\u95f4\u6253\u5370\u4ec0\u4e48\u6765\u9694\u5f00\u5b83\u4eec*<\/p>\n
>><\/span>> print('dog'<\/span>,'cat'<\/span>,'mice'<\/span>)\r\ndog cat mice\r\n>><\/span>> print('dog'<\/span>,'cat'<\/span>,'mice'<\/span>,sep=','<\/span>)\r\ndog,cat,mice\r\n>><\/span>> \r\n\r\n<\/code><\/pre>\n3.5 \u5c40\u90e8\u4f5c\u7528\u57df<\/strong><\/strong><\/span><\/div>\n\u5728\u88ab\u8c03\u7528\u51fd\u6570\u5185\u8d4b\u503c\u7684\u53d8\u5143\u548c\u53d8\u91cf\uff0c\u5904\u4e8e\u8be5\u51fd\u6570\u7684\u201c\u5c40\u90e8\u4f5c\u7528\u57df\u201d\uff0c\u5728\u6240\u6709\u51fd\u6570\u4e4b\u5916\u590d\u5236\u7684\u53d8\u91cf\uff0c\u5c5e\u4e8e\u201c\u5168\u5c40\u4f5c\u7528\u57df\u201d\u3002\u5904\u4e8e\u5c40\u90e8\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u88ab\u79f0\u4e3a\u201c\u5c40\u90e8\u53d8\u91cf\u201d\u3002\u5904\u4e8e\u5168\u5c40\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u88ab\u79f0\u4e3a\u201c\u5168\u5c40\u53d8\u91cf\u201d\u3002\u4e00\u4e2a\u53d8\u91cf\u5fc5\u662f\u5176\u4e2d\u7684\u4e00\u79cd\uff0c\u4e0d\u80fd\u65e2\u662f\u5168\u5c40\u6709\u4e8b\u5c40\u90e8\u7684\u3002
\n\u53ef\u4ee5\u5c06\u201c\u4f5c\u7528\u57df\u201d\u770b\u6210\u662f\u53d8\u91cf\u7684\u5bb9\u5668\u3002\u5f53\u4f5c\u7528\u57df\u88ab\u9500\u6bc1\u65f6\uff0c\u6240\u6709\u4fdd\u5b58\u5728\u8be5\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\u503c\u5c31\u88ab\u4e22\u5f03\u4e86\u3002<\/p>\n
\u4f5c\u7528\u57df\u5f88\u91cd\u8981\uff0c\u7406\u7531\u5982\u4e0b\uff1a<\/p>\n
\n- \u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u4ee3\u7801\u4e0d\u80fd\u4f7f\u7528\u4efb\u4f55\u5c40\u90e8\u53d8\u91cf<\/li>\n
- \u4f46\u662f\uff0c\u5c40\u90e8\u4f5c\u7528\u57df\u53ef\u4ee5\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf<\/li>\n
- \u4e00\u4e2a\u51fd\u6570\u7684\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u4ee3\u7801\uff0c\u4e0d\u80fd\u4f7f\u7528\u5176\u4ed6\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf<\/li>\n
- \u5982\u679c\u5728\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u4e2d\uff0c\u4f60\u53ef\u4ee5\u7528\u76f8\u540c\u7684\u540d\u5b57\u547d\u540d\u4e0d\u540c\u7684\u53d8\u91cf<\/li>\n<\/ul>\n
3.5.1 \u5c40\u90e8\u53d8\u91cf\u4e0d\u80fd\u5728\u5168\u5c40\u4f5c\u7528\u57df\u5185\u4f7f\u7528<\/strong><\/p>\ndef<\/span> spam<\/span>():\r\n eggs<\/span> = 111<\/span>\r\nspam<\/span>()<\/span>\r\nprint<\/span>(eggs<\/span>)<\/span>\r\n<\/code><\/pre>\n\u5982\u679c\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u5c31\u4f1a\u51fa\u9519\uff0ceggs\u53d8\u91cf\u53ea\u5c5e\u4e8espam\uff08\uff09\u8c03\u7528\u6240\u521b\u5efa\u7684\u5c40\u90e8\u4f5c\u7528\u57df\u3002<\/p>\n
3.5.2 \u5c40\u90e8\u4f5c\u7528\u57df\u4e0d\u80fd\u4f7f\u7528\u5176\u4ed6\u5c40\u90e8\u4f5c\u7528\u5185\u7684\u53d8\u91cf<\/strong><\/p>\ndef<\/span> spam<\/span>():\r\n eggs<\/span> = 99<\/span>\r\n bacon<\/span>()<\/span>\r\n print<\/span>(eggs<\/span>)<\/span>\r\n \r\ndef<\/span> bacon<\/span>()<\/span>\r\n ham<\/span> = 101<\/span>\r\n eggs<\/span> = 0<\/span>\r\n \r\nspam<\/span>()<\/span>\r\n<\/code><\/pre>\n\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0cspam()\u88ab\u8c03\u7528\uff0c\u521b\u5efa\u4e00\u4e2a\u5c40\u90e8\u4f5c\u7528\u57df\u3002\u5c40\u90e8\u53d8\u91cfeggs\u88ab\u8d4b\u503c\u4e3a99\u3002\u7136\u540ebacon()\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u521b\u5efa\u7b2c\u4e8c\u4e2a\u4f5c\u7528\u57df\u3002\u591a\u4e2a\u5c40\u90e8\u4f5c\u7528\u57df\u80fd\u540c\u65f6\u5b58\u5728\u3002\u5728\u8fd9\u65b0\u7684\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\uff0c\u5c40\u90e8\u53d8\u91cfham\u4e5f\u88ab\u521b\u5efa\u88ab\u8d4b\u503c\u4e3a101\u3002\u5c40\u90e8\u53d8\u91cfeggs\u4e5f\u88ab\u521b\u5efa\u5e76\u8d4b\u503c\u4e3a0\u3002
\n\u5f53bacon()\u8fd4\u56de\u65f6\uff0c\u8fd9\u6b21\u8c03\u7528\u7684\u5c40\u90e8\u4f5c\u7528\u57df\u88ab\u9500\u6bc1\u3002\u7a0b\u5e8f\u6267\u884c\u5728spam()\u51fd\u6570\u4e2d\u7ee7\u7eed\uff0c\u6253\u5370\u51faeggs\u7684\u503c\u3002\u8fd9\u6b21spam()\u8c03\u7528\u7684\u5c40\u90e8\u4f5c\u7528\u57df\u4efb\u7136\u5b58\u5728\uff0ceggs\u88ab\u8d4b\u503c\u4e3a99.<\/p>\n
3.5.3 \u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u5728\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u8bfb\u53d6<\/strong><\/p>\ndef<\/span> spam<\/span>():\r\n print<\/span>(eggs<\/span>)<\/span>\r\neggs<\/span> = 42<\/span>\r\nspam<\/span>()<\/span>\r\nprint<\/span>(eggs<\/span>)<\/span>\r\n<\/code><\/pre>\n3.6 global \u8bed\u53e5<\/strong><\/strong><\/span><\/div>\n\u5982\u679c\u9700\u8981\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u5c31\u662f\u7528global\u8bed\u53e5<\/p>\n
\u6709\u56db\u6761\u6cd5\u5219\uff0c\u6765\u533a\u5206\u4e00\u4e2a\u53d8\u91cf\u662f\u5904\u4e8e\u5c40\u90e8\u4f5c\u7528\u57df\u8fd8\u662f\u5168\u5c40\u4f5c\u7528\u57df<\/p>\n
\n- \u5982\u679c\u53d8\u91cf\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528\uff08\u5373\u5728\u6240\u6709\u51fd\u6570\u4e4b\u5916\uff09\uff0c\u5b83\u5c31\u662f\u5168\u5c40\u53d8\u91cf<\/li>\n
- \u5982\u679c\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u6709\u9488\u5bf9\u8be5\u53d8\u91cf\u7684global\u8bed\u53e5\uff0c\u5b83\u5c31\u662f\u5168\u5c40\u53d8\u91cf<\/li>\n
- \u5426\u5219\uff0c\u5982\u679c\u8be5\u53d8\u91cf\u7528\u4e8e\u51fd\u6570\u4e2d\u7684\u8d4b\u503c\u8bed\u53e5\uff0c\u5b83\u5c31\u662f\u5c40\u90e8\u53d8\u91cf<\/li>\n
- \u4f46\u662f\uff0c\u5982\u679c\u8be5\u53d8\u91cf\u6ca1\u6709\u7528\u5728\u8d4b\u503c\u8bed\u53e5\u4e2d\uff0c\u5b83\u5c31\u662f\u5168\u5c40\u53d8\u91cf<\/li>\n<\/ul>\n3.7 \u5f02\u5e38\u5904\u7406<\/strong><\/strong><\/span><\/div>\n
\u5f53\u7a0b\u5e8f\u8fd0\u884c\u51fa\u9519\u65f6\uff0c\u53ef\u4ee5\u7528try\u548cexcept\u8bed\u53e5\u6765\u5904\u7406\u3002\u90a3\u4e9b\u53ef\u80fd\u51fa\u9519\u7684\u8bed\u53e5\u88ab\u653e\u5728try\u5b50\u53e5\u4e2d\u3002\u5982\u679c\u9519\u8bef\u53d1\u751f\uff0c\u7a0b\u5e8f\u6267\u884c\u5c31\u8f6c\u5230\u63a5\u4e0b\u6765\u7684except\u5b50\u53e5\u5f00\u59cb\u5904\u3002<\/p>\n
\u5982\u679ctry\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801\u5bfc\u81f4\u4e00\u4e2a\u9519\u8bef\uff0c\u7a0b\u5e8f\u6267\u884c\u5c31\u7acb\u5373\u8f6c\u5230except\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801\uff0c\u4e00\u65e6\u6267\u884c\u8df3\u5230except\u5b50\u53e5\u7684\u4ee3\u7801\u4e2d\uff0c\u5c31\u4e0d\u4f1a\u56de\u5230try\u7684\u5b50\u53e5\u4e2d\u4e86<\/p>\n
3.8 \u4e00\u4e2a\u5c0f\u7a0b\u5e8f\uff1a\u731c\u6570\u5b57<\/strong><\/strong><\/span><\/div>\nimport<\/span> random<\/span>\r\n\r\nsecretNumber = random<\/span>.randint(1<\/span>,20<\/span>)\r\nprint<\/span>('i am think of number between 1 and 20:'<\/span>)\r\n\r\nfor<\/span> guessesTaken in range(1<\/span>,7<\/span>):\r\n print<\/span>('take a guess'<\/span>)\r\n guess = int<\/span>(input())\r\n\r\n if<\/span> guess < secretNumber:\r\n print<\/span>('you guess is too low.'<\/span>)\r\n elif guess > secretNumber:\r\n print<\/span>('you guess is too high'<\/span>)\r\n else<\/span>:\r\n break<\/span>\r\nif<\/span> guess == secretNumber:\r\n print<\/span>('good job!the number i was thinking of was '<\/span> + str<\/span>(secretNumber)+\r\n ' and you guessed my number in '<\/span> + str<\/span>(guessesTaken) + ' guesses'<\/span>)\r\nelse<\/span>:\r\n print<\/span>('Nope. the number i was thinking of was '<\/span> + str<\/span>(secretNumber))\r\n\r\n<\/code><\/pre>\n3.9 \u5b9e\u8df5<\/strong><\/strong><\/span><\/div>\nCollatz \u5e8f\u5217 \u7f16\u5199\u4e00\u4e2a\u540d\u4e3a collatz()\u7684\u51fd\u6570\uff0c\u5b83\u6709\u4e00\u4e2a\u540d\u4e3a number\u7684\u53c2\u6570\u3002\u5982\u679c\u53c2\u6570\u662f\u5076\u6570\uff0c\u90a3\u4e48 collatz()\u5c31\u6253\u5370\u51fa number \/\/ 2\uff0c\u5e76\u8fd4\u56de\u8be5\u503c\u3002\u5982\u679c number\u662f\u5947\u6570\uff0ccollatz()\u5c31\u6253\u5370\u5e76\u8fd4\u56de 3 * number + 1\u3002<\/p>\n
\u7136\u540e\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u8ba9\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff0c\u5e76\u4e0d\u65ad\u5bf9\u8fd9\u4e2a\u6570\u8c03\u7528collatz()\uff0c\u76f4\u5230\u51fd\u6570\u8fd4\u56de\u503c\uff11\uff08\u4ee4\u4eba\u60ca\u5947\u7684\u662f\uff0c\u8fd9\u4e2a\u5e8f\u5217\u5bf9\u4e8e\u4efb\u4f55\u6574\u6570\u90fd\u6709\u6548\uff0c\u5229\u7528\u8fd9\u4e2a\u5e8f\u5217\uff0c \u4f60\u8fdf\u65e9\u4f1a\u5f97\u5230 1\uff01\u65e2\u4f7f\u6570\u5b66\u5bb6\u4e5f\u4e0d\u80fd\u786e\u5b9a\u4e3a\u4ec0\u4e48\u3002\u4f60\u7684\u7a0b\u5e8f\u5728\u7814\u7a76\u6240\u8c13\u7684\u201cCollatz \u5e8f\u5217\u201d\uff0c\u5b83\u6709\u65f6\u5019\u88ab\u79f0\u4e3a\u201c\u6700\u7b80\u5355\u7684\u3001\u4e0d\u53ef\u80fd\u7684\u6570\u5b66\u95ee\u9898\u201d\uff09\u3002<\/p>\n
\u5728\u524d\u9762\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0 try \u548c except \u8bed\u53e5\uff0c\u68c0\u6d4b\u7528\u6237\u662f\u5426\u8f93\u5165\u4e86\u4e00\u4e2a\u975e\u6574\u6570\u7684\u5b57\u7b26\u4e32\u3002\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0cint()\u51fd\u6570\u5728\u4f20\u5165\u4e00\u4e2a\u975e\u6574\u6570\u5b57\u7b26\u4e32\u65f6\uff0c\u4f1a\u4ea7\u751f ValueError \u9519\u8bef\uff0c\u6bd4\u5982 int('puppy')\u3002\u5728 except\u5b50\u53e5\u4e2d\uff0c\u5411\u7528\u6237\u8f93\u51fa\u4e00\u6761\u4fe1\u606f\uff0c\u544a\u8bc9\u4ed6\u4eec\u5fc5\u987b\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n
\u793a\u4f8b\u4ee3\u78011\uff1a<\/p>\n
def collatz(number<\/span>):\r\n if<\/span> number<\/span> == 1<\/span>:\r\n return<\/span> 1<\/span>\r\n elif number<\/span> % 2<\/span> == 0<\/span>:\r\n numbers = number<\/span>\/\/2<\/span>\r\n print<\/span>(numbers)\r\n collatz(numbers)\r\n elif number<\/span> % 2<\/span> == 1<\/span>:\r\n numbers = 3<\/span>*number<\/span>+1<\/span>\r\n print<\/span>(numbers)\r\n collatz(numbers)\r\n\r\n\r\nprint<\/span>('please input a number'<\/span>)\r\ntry<\/span>:\r\n number<\/span> = int<\/span>(input<\/span>())\r\n collatz(number<\/span>)\r\nexcept ValueError:\r\n print<\/span>('please input a integer number'<\/span>)\r\n\r\n\r\n\r\n<\/code><\/pre>\n\u793a\u4f8b\u4ee3\u7801 2\uff1a<\/p>\n
def collatz(number<\/span>):\r\n\r\n if<\/span> number<\/span> % 2<\/span> == 0<\/span>:\r\n numbers = number<\/span>\/\/2<\/span>\r\n return<\/span> numbers\r\n else<\/span>:\r\n numbers = 3<\/span>*number<\/span>+1<\/span>\r\n return<\/span> numbers\r\n\r\n\r\nprint<\/span>('please input a integer number'<\/span>)\r\ntry<\/span>:\r\n number<\/span>=int<\/span>(input<\/span>())\r\n while<\/span> collatz(number<\/span>) != 1<\/span>:\r\n print<\/span>(collatz(number<\/span>))\r\n new_number=collatz(number<\/span>)\r\n number<\/span> = new_number\r\n collatz(number<\/span>)\r\n print<\/span>(collatz(number<\/span>))\r\nexcept ValueError:\r\n print<\/span>('please input a integer'<\/span>)\r\n\r\n<\/code><\/pre>\n\u3010\u6ce8\u3011\u8fd9\u4e24\u4e2a\u90fd\u662f\u6211\u81ea\u5df1\u5199\u7684 \u611f\u89c9\u7b2c\u4e00\u4e2a\u4f1a\u6bd4\u8f83\u597d\u4e00\u70b9<\/p>\n","protected":false},"excerpt":{"rendered":"
\u201c\u51fd\u6570\u201d\u5c31\u50cf\u4e00\u4e2a\u7a0b\u5e8f\u5185\u7684\u5c0f\u7a0b\u5e8f \u5982\u679c\u8c03\u7528print()\u6216\u8005len()\u51fd\u6570\uff0c\u4f60\u4f1a\u4f20\u5165\u4e00\u4e9b\u503c\uff0c\u653e\u5728\u62ec\u53f7\u4e4b\u95f4\uff0c\u5728\u8fd9\u91cc […]<\/p>\n","protected":false},"author":1476,"featured_media":148670,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-148644","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/148644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1476"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=148644"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/148644\/revisions"}],"predecessor-version":[{"id":148770,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/148644\/revisions\/148770"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/148670"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=148644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=148644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=148644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}