{"id":958034,"date":"2024-12-27T03:14:05","date_gmt":"2024-12-26T19:14:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958034.html"},"modified":"2024-12-27T03:14:09","modified_gmt":"2024-12-26T19:14:09","slug":"turnleft%e5%9c%a8python%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/958034.html","title":{"rendered":"turnleft\u5728python\u5982\u4f55\u7f16\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101155\/bb7108e6-f1f5-455d-a624-f9863fbf83ad.webp\" alt=\"turnleft\u5728python\u5982\u4f55\u7f16\u5199\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<code>turnleft<\/code>\u901a\u5e38\u662f\u7528\u4e8e\u63a7\u5236\u673a\u5668\u4eba\u6216\u6a21\u62df\u5668\u4e2d\u7684\u6307\u4ee4\u3002\u8981\u5b9e\u73b0\u8fd9\u6837\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u60a8\u6b63\u5728\u5904\u7406\u7684\u7cfb\u7edf\u6216\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u7684\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u63a7\u5236\u65b9\u5411\u3001\u5229\u7528\u73b0\u6709\u7684\u673a\u5668\u4eba\u5e93\u5982turtle\u3001\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u5de6\u8f6c\u529f\u80fd<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Python\u5185\u7f6e\u7684<code>turtle<\/code>\u5e93\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u6a21\u62df\u65b9\u5411\u63a7\u5236\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>turtle<\/code>\u5e93\u5b9e\u73b0<code>turnleft<\/code>\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528turtle\u5e93\u5b9e\u73b0turnleft<\/h3>\n<\/p>\n<p><p><code>turtle<\/code>\u5e93\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u7ed8\u56fe\u548c\u6a21\u62df\u7b80\u5355\u7684\u79fb\u52a8\u3002\u901a\u8fc7\u4f7f\u7528<code>turtle<\/code>\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5de6\u8f6c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165turtle\u5e93<\/h4>\n<\/p>\n<p><p><code>turtle<\/code>\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u56e0\u6b64\u5728\u5927\u591a\u6570Python\u5b89\u88c5\u4e2d\u9ed8\u8ba4\u53ef\u7528\u3002\u8981\u4f7f\u7528\u5b83\uff0c\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521d\u59cb\u5316turtle\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>turtle<\/code>\u5e93\u65f6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>turtle<\/code>\u5bf9\u8c61\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>turtle.Turtle()<\/code>\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pen = turtle.Turtle()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pen<\/code>\u662f\u4e00\u4e2a<code>turtle<\/code>\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u63a7\u5236\u65b9\u5411\u3001\u79fb\u52a8\u548c\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><h4>3. \u5b9e\u73b0turnleft\u529f\u80fd<\/h4>\n<\/p>\n<p><p>\u8981\u5b9e\u73b0\u5de6\u8f6c\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>turtle<\/code>\u5bf9\u8c61\u7684<code>left<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u8981\u5de6\u8f6c\u7684\u89d2\u5ea6\u3002\u4f8b\u5982\uff0c\u8981\u8ba9<code>turtle<\/code>\u5bf9\u8c61\u5de6\u8f6c90\u5ea6\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pen.left(90)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u8ba9<code>turtle<\/code>\u5bf9\u8c61\u5de6\u8f6c\u5e76\u5411\u524d\u79fb\u52a8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521d\u59cb\u5316turtle\u5bf9\u8c61<\/strong><\/h2>\n<p>pen = turtle.Turtle()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u901f\u5ea6<\/strong><\/h2>\n<p>pen.speed(1)<\/p>\n<h2><strong>\u5411\u524d\u79fb\u52a8100\u4e2a\u5355\u4f4d<\/strong><\/h2>\n<p>pen.forward(100)<\/p>\n<h2><strong>\u5de6\u8f6c90\u5ea6<\/strong><\/h2>\n<p>pen.left(90)<\/p>\n<h2><strong>\u518d\u6b21\u5411\u524d\u79fb\u52a8100\u4e2a\u5355\u4f4d<\/strong><\/h2>\n<p>pen.forward(100)<\/p>\n<h2><strong>\u9690\u85cfturtle<\/strong><\/h2>\n<p>pen.hideturtle()<\/p>\n<h2><strong>\u5173\u95ed\u7ed8\u56fe\u7a97\u53e3<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u5de6\u8f6c\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u60a8\u8fd8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u5de6\u8f6c\u529f\u80fd\uff0c\u4ee5\u4fbf\u5728\u4ee3\u7801\u4e2d\u66f4\u76f4\u89c2\u5730\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9a\u4e49turnleft\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>turnleft<\/code>\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a<code>turtle<\/code>\u5bf9\u8c61\u548c\u4e00\u4e2a\u89d2\u5ea6\u4f5c\u4e3a\u53c2\u6570\u3002\u901a\u8fc7\u8c03\u7528<code>turtle<\/code>\u5bf9\u8c61\u7684<code>left<\/code>\u65b9\u6cd5\u5b9e\u73b0\u5de6\u8f6c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def turnleft(turtle_obj, angle):<\/p>\n<p>    turtle_obj.left(angle)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528turnleft\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u60a8\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>turnleft<\/code>\u51fd\u6570\u6765\u63a7\u5236<code>turtle<\/code>\u5bf9\u8c61\u7684\u65b9\u5411\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521d\u59cb\u5316turtle\u5bf9\u8c61<\/strong><\/h2>\n<p>pen = turtle.Turtle()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u901f\u5ea6<\/strong><\/h2>\n<p>pen.speed(1)<\/p>\n<h2><strong>\u5411\u524d\u79fb\u52a8100\u4e2a\u5355\u4f4d<\/strong><\/h2>\n<p>pen.forward(100)<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5de6\u8f6c90\u5ea6<\/strong><\/h2>\n<p>turnleft(pen, 90)<\/p>\n<h2><strong>\u518d\u6b21\u5411\u524d\u79fb\u52a8100\u4e2a\u5355\u4f4d<\/strong><\/h2>\n<p>pen.forward(100)<\/p>\n<h2><strong>\u9690\u85cfturtle<\/strong><\/h2>\n<p>pen.hideturtle()<\/p>\n<h2><strong>\u5173\u95ed\u7ed8\u56fe\u7a97\u53e3<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5728\u673a\u5668\u4eba\u7f16\u7a0b\u4e2d\u5b9e\u73b0turnleft<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u6b63\u5728\u5904\u7406\u673a\u5668\u4eba\u7f16\u7a0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u6216API\u6765\u5b9e\u73b0\u5de6\u8f6c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u673a\u5668\u4eba\u7f16\u7a0b\u5e93\u548c\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528ROS\uff08\u673a\u5668\u4eba\u64cd\u4f5c\u7cfb\u7edf\uff09<\/h4>\n<\/p>\n<p><p>\u5728ROS\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528\u8bdd\u9898\uff08topics\uff09\u548c\u670d\u52a1\uff08services\uff09\u6765\u63a7\u5236\u673a\u5668\u4eba\u7684\u8fd0\u52a8\u3002\u8981\u5b9e\u73b0\u5de6\u8f6c\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u53d1\u5e03\u4e00\u4e2a\u5305\u542b\u901f\u5ea6\u548c\u65b9\u5411\u7684\u6d88\u606f\u5230\u7279\u5b9a\u7684\u8bdd\u9898\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528EV3 Python\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u6b63\u5728\u4f7f\u7528\u4e50\u9ad8EV3\u673a\u5668\u4eba\uff0c\u53ef\u4ee5\u4f7f\u7528EV3 Python\u5e93\u6765\u63a7\u5236\u673a\u5668\u4eba\u7684\u8fd0\u52a8\u3002\u8981\u5b9e\u73b0\u5de6\u8f6c\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u7535\u673a\u901f\u5ea6\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u51cf\u5c0f\u53f3\u4fa7\u7535\u673a\u7684\u901f\u5ea6\u6216\u589e\u52a0\u5de6\u4fa7\u7535\u673a\u7684\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ev3dev.ev3 import *<\/p>\n<h2><strong>\u521d\u59cb\u5316\u7535\u673a<\/strong><\/h2>\n<p>left_motor = LargeMotor(&#39;outB&#39;)<\/p>\n<p>right_motor = LargeMotor(&#39;outC&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u5de6\u8f6c\u51fd\u6570<\/strong><\/h2>\n<p>def turnleft(speed, duration):<\/p>\n<p>    left_motor.run_timed(speed_sp=speed, time_sp=duration)<\/p>\n<p>    right_motor.run_timed(speed_sp=speed\/\/2, time_sp=duration)<\/p>\n<h2><strong>\u4ee5500\u7684\u901f\u5ea6\u5de6\u8f6c2\u79d2<\/strong><\/h2>\n<p>turnleft(500, 2000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0<code>turnleft<\/code>\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u5229\u7528<code>turtle<\/code>\u5e93\u6765\u6a21\u62df\u65b9\u5411\u63a7\u5236\uff0c\u4e5f\u53ef\u4ee5\u5728\u673a\u5668\u4eba\u7f16\u7a0b\u4e2d\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u548cAPI\u6765\u5b9e\u73b0\u3002<strong>\u5173\u952e\u5728\u4e8e\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u5e93\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u63a7\u5236\u5bf9\u8c61\u7684\u8fd0\u52a8\u65b9\u5411<\/strong>\u3002\u901a\u8fc7\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5de6\u8f6c\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528turnleft\u547d\u4ee4\uff1f<\/strong><br \/>turnleft\u5e76\u4e0d\u662fPython\u5185\u7f6e\u7684\u547d\u4ee4\uff0c\u800c\u662f\u67d0\u4e9b\u7f16\u7a0b\u73af\u5883\u6216\u5e93\u4e2d\u4f7f\u7528\u7684\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u7279\u5b9a\u7684\u5e93\uff08\u4f8b\u5982Turtle\u6a21\u5757\uff09\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u5728Turtle\u5e93\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>left(angle)<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u5411\u5de6\u8f6c\u52a8\u7684\u6548\u679c\u3002\u8c03\u7528\u793a\u4f8b\uff1a<code>t.left(90)<\/code>\u4f1a\u4f7f\u56fe\u5f62\u5411\u5de6\u8f6c90\u5ea6\u3002<\/p>\n<p><strong>\u6211\u8be5\u5982\u4f55\u5b89\u88c5Python\u7684Turtle\u5e93\uff1f<\/strong><br \/>Turtle\u5e93\u901a\u5e38\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u4f60\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u5b83\u3002\u53ea\u9700\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u6b63\u786e\u5b89\u88c5\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7Python\u7684\u547d\u4ee4\u884c\u6216IDLE\u76f4\u63a5\u5bfc\u5165Turtle\u5e93\uff0c\u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a<code>import turtle<\/code>\uff0c\u63a5\u7740\u4fbf\u53ef\u4ee5\u4f7f\u7528\u4e0eturnleft\u76f8\u4f3c\u7684\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0turnleft\u529f\u80fd\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4f7f\u7528\u7c7b\u4f3cturnleft\u7684\u529f\u80fd\u5e7f\u6cdb\u5e94\u7528\u4e8e\u56fe\u5f62\u7ed8\u5236\u3001\u6e38\u620f\u5f00\u53d1\u548c\u673a\u5668\u4eba\u7f16\u7a0b\u7b49\u573a\u666f\u3002\u5728\u8fd9\u4e9b\u9886\u57df\u4e2d\uff0c\u63a7\u5236\u5bf9\u8c61\u7684\u65b9\u5411\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728Turtle\u7ed8\u56fe\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7turnleft\u5b9e\u73b0\u56fe\u5f62\u7684\u65cb\u8f6c\u548c\u79fb\u52a8\uff0c\u6216\u8005\u5728\u6e38\u620f\u4e2d\u63a7\u5236\u89d2\u8272\u7684\u65b9\u5411\u53d8\u5316\u4ee5\u5e94\u5bf9\u4e0d\u540c\u7684\u73af\u5883\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cturnleft\u901a\u5e38\u662f\u7528\u4e8e\u63a7\u5236\u673a\u5668\u4eba\u6216\u6a21\u62df\u5668\u4e2d\u7684\u6307\u4ee4\u3002\u8981\u5b9e\u73b0\u8fd9\u6837\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5177 [&hellip;]","protected":false},"author":3,"featured_media":958044,"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\/958034"}],"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=958034"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958034\/revisions"}],"predecessor-version":[{"id":958045,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958034\/revisions\/958045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/958044"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=958034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=958034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=958034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}