鱼C论坛

 找回密码
 立即注册
查看: 2248|回复: 9

[已解决]各位大佬,这个为什么错了呀,怎么修改呢

[复制链接]
发表于 2021-3-11 11:04:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import turtle
  2. turtle.setup(800,800)
  3. turtle.pensize(5)
  4. turtle.pencolor('green')
  5. turtle.speed('fastest')
  6. turtle.penup()
  7. turtle.goto(50,-200)
  8. turtle.pendown()
  9. turtle.fillcolor('green')
  10. turtle.begin_fill()
  11. turtle.fd(200)
  12. turtle.seth(130)
  13. turtle.fd(220)
  14. turtle.rt(130)
  15. turtle.fd(120)
  16. turtle.seth(130)
  17. turtle.fd(200)
  18. turtle.rt(130)
  19. turtle.fd(100)
  20. turtle.seth(130)
  21. turtle.fd(300)
  22. turtle.lt(100)
  23. turtle.fd(300)
  24. turtle.lt(130)
  25. turtle.fd(100)
  26. turtle.rt(130)
  27. turtle.fd(200)
  28. turtle.lt(130)
  29. turtle.fd(120)
  30. turtle.rt(130)
  31. turtle.fd(220)
  32. turtle.lt(130)
  33. turtle.fd(200)
  34. turtle.rt(90)
  35. turtle.fd(100)
  36. turtle.lt(90)
  37. turtle.fd(85)
  38. turtle.lt(90)
  39. turtle.fd(100)
  40. turtle.end_fill()
  41. turtle.penup()
  42. turtle.goto(260,-200)#右下第一个圆
  43. turtle.pendown()
  44. turtle.fillcolor('yellow')
  45. turtle.begin_fill()
  46. turtle.pencolor('yellow')
  47. turtle.circle(10,360)
  48. turtle.penup()
  49. turtle.goto(244,-28)#右边第二个圆
  50. turtle.pendown()
  51. turtle.circle(10,360)
  52. turtle.end_fill()
  53. turtle.penup()
  54. turtle.goto(210,130)#右边第三个圆
  55. turtle.pendown()
  56. turtle.fillcolor('yellow')
  57. turtle.begin_fill()
  58. turtle.pencolor('yellow')
  59. turtle.circle(10,360)
  60. turtle.end_fill()
  61. turtle.penup()
  62. turtle.goto(-180,130)#左边第三个圆
  63. turtle.pendown()
  64. turtle.fillcolor('yellow')
  65. turtle.begin_fill()
  66. turtle.pencolor('yellow')
  67. turtle.circle(10,360)
  68. turtle.end_fill()
  69. turtle.penup()
  70. turtle.goto(-214,-28)#左边第二个圆
  71. turtle.pendown()
  72. turtle.fillcolor('yellow')
  73. turtle.begin_fill()
  74. turtle.pencolor('yellow')
  75. turtle.circle(10,360)
  76. turtle.penup()
  77. turtle.goto(-230,-200)#左边第一个圆
  78. turtle.pendown()
  79. turtle.circle(10,360)
  80. turtle.end_fill()
  81. turtle.penup()
  82. turtle.goto(-5,325)#最上面的星星
  83. turtle.seth(80)
  84. turtle.pendown()
  85. turtle.fillcolor('tomato')
  86. turtle.begin_fill()
  87. turtle.pencolor('tomato')
  88. while True:
  89.     turtle.fd(50)
  90.     turtle.rt(144)
  91.     turtle.fd(50)
  92.     turtle.rt(144)
  93.     turtle.fd(50)
  94.     turtle.rt(144)
  95.     turtle.fd(50)
  96.     turtle.rt(144)
  97.     turtle.fd(50)
  98.     turtle.rt(144)
  99.     if abs(pos())<1:
  100.         break
  101. end_fill()
复制代码
最佳答案
2021-3-11 12:02:03
guhusf 发表于 2021-3-11 11:42
最上面画笔还是会一直循环

一直不满足条件,不会跳出,这里类似于死循环,可以加个print(abs(turtle.pos()))来监控

  1. import turtle
  2. turtle.setup(800,800)
  3. turtle.pensize(5)
  4. turtle.pencolor('green')
  5. turtle.speed('fastest')
  6. turtle.penup()
  7. turtle.goto(50,-200)
  8. turtle.pendown()
  9. turtle.fillcolor('green')
  10. turtle.begin_fill()
  11. turtle.fd(200)
  12. turtle.seth(130)
  13. turtle.fd(220)
  14. turtle.rt(130)
  15. turtle.fd(120)
  16. turtle.seth(130)
  17. turtle.fd(200)
  18. turtle.rt(130)
  19. turtle.fd(100)
  20. turtle.seth(130)
  21. turtle.fd(300)
  22. turtle.lt(100)
  23. turtle.fd(300)
  24. turtle.lt(130)
  25. turtle.fd(100)
  26. turtle.rt(130)
  27. turtle.fd(200)
  28. turtle.lt(130)
  29. turtle.fd(120)
  30. turtle.rt(130)
  31. turtle.fd(220)
  32. turtle.lt(130)
  33. turtle.fd(200)
  34. turtle.rt(90)
  35. turtle.fd(100)
  36. turtle.lt(90)
  37. turtle.fd(85)
  38. turtle.lt(90)
  39. turtle.fd(100)
  40. turtle.end_fill()
  41. turtle.penup()
  42. turtle.goto(260,-200)#右下第一个圆
  43. turtle.pendown()
  44. turtle.fillcolor('yellow')
  45. turtle.begin_fill()
  46. turtle.pencolor('yellow')
  47. turtle.circle(10,360)
  48. turtle.penup()
  49. turtle.goto(244,-28)#右边第二个圆
  50. turtle.pendown()
  51. turtle.circle(10,360)
  52. turtle.end_fill()
  53. turtle.penup()
  54. turtle.goto(210,130)#右边第三个圆
  55. turtle.pendown()
  56. turtle.fillcolor('yellow')
  57. turtle.begin_fill()
  58. turtle.pencolor('yellow')
  59. turtle.circle(10,360)
  60. turtle.end_fill()
  61. turtle.penup()
  62. turtle.goto(-180,130)#左边第三个圆
  63. turtle.pendown()
  64. turtle.fillcolor('yellow')
  65. turtle.begin_fill()
  66. turtle.pencolor('yellow')
  67. turtle.circle(10,360)
  68. turtle.end_fill()
  69. turtle.penup()
  70. turtle.goto(-214,-28)#左边第二个圆
  71. turtle.pendown()
  72. turtle.fillcolor('yellow')
  73. turtle.begin_fill()
  74. turtle.pencolor('yellow')
  75. turtle.circle(10,360)
  76. turtle.penup()
  77. turtle.goto(-230,-200)#左边第一个圆
  78. turtle.pendown()
  79. turtle.circle(10,360)
  80. turtle.end_fill()
  81. turtle.penup()
  82. turtle.goto(-5,325)#最上面的星星
  83. turtle.seth(80)
  84. turtle.pendown()
  85. turtle.fillcolor('tomato')
  86. turtle.begin_fill()
  87. turtle.pencolor('tomato')
  88. while True:
  89.     turtle.fd(50)
  90.     turtle.rt(144)
  91.     turtle.fd(50)
  92.     turtle.rt(144)
  93.     turtle.fd(50)
  94.     turtle.rt(144)
  95.     turtle.fd(50)
  96.     turtle.rt(144)
  97.     turtle.fd(50)
  98.     turtle.rt(144)
  99.     print(abs(turtle.pos()))
  100.     if abs(turtle.pos())<1:
  101.         break
  102. turtle.end_fill()
复制代码

报错界面

报错界面
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-11 11:06:37 | 显示全部楼层
pos没有定义,很明显啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-11 11:17:48 | 显示全部楼层
巴巴鲁 发表于 2021-3-11 11:06
pos没有定义,很明显啊

怎么定义啊,这些程序都是照着书抄的,还不会自己写
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-11 11:24:23 | 显示全部楼层
pos()改成turtle.pos()
这是turtle库的pos函数:abs(pos())<1: 可理解为 画笔所处位置的坐标,距离原点(0,0)的距离的绝对值小于1,顾名思义就是用来查看 画笔经过若干次循环后 是否回到原点,回到原点即为真(True)。
若回到原点,便可用 break 跳出循环,使画笔停止循环运动。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-11 11:31:31 | 显示全部楼层
  1. import turtle
  2. turtle.setup(800,800)
  3. turtle.pensize(5)
  4. turtle.pencolor('green')
  5. turtle.speed('fastest')
  6. turtle.penup()
  7. turtle.goto(50,-200)
  8. turtle.pendown()
  9. turtle.fillcolor('green')
  10. turtle.begin_fill()
  11. turtle.fd(200)
  12. turtle.seth(130)
  13. turtle.fd(220)
  14. turtle.rt(130)
  15. turtle.fd(120)
  16. turtle.seth(130)
  17. turtle.fd(200)
  18. turtle.rt(130)
  19. turtle.fd(100)
  20. turtle.seth(130)
  21. turtle.fd(300)
  22. turtle.lt(100)
  23. turtle.fd(300)
  24. turtle.lt(130)
  25. turtle.fd(100)
  26. turtle.rt(130)
  27. turtle.fd(200)
  28. turtle.lt(130)
  29. turtle.fd(120)
  30. turtle.rt(130)
  31. turtle.fd(220)
  32. turtle.lt(130)
  33. turtle.fd(200)
  34. turtle.rt(90)
  35. turtle.fd(100)
  36. turtle.lt(90)
  37. turtle.fd(85)
  38. turtle.lt(90)
  39. turtle.fd(100)
  40. turtle.end_fill()
  41. turtle.penup()
  42. turtle.goto(260,-200)#右下第一个圆
  43. turtle.pendown()
  44. turtle.fillcolor('yellow')
  45. turtle.begin_fill()
  46. turtle.pencolor('yellow')
  47. turtle.circle(10,360)
  48. turtle.penup()
  49. turtle.goto(244,-28)#右边第二个圆
  50. turtle.pendown()
  51. turtle.circle(10,360)
  52. turtle.end_fill()
  53. turtle.penup()
  54. turtle.goto(210,130)#右边第三个圆
  55. turtle.pendown()
  56. turtle.fillcolor('yellow')
  57. turtle.begin_fill()
  58. turtle.pencolor('yellow')
  59. turtle.circle(10,360)
  60. turtle.end_fill()
  61. turtle.penup()
  62. turtle.goto(-180,130)#左边第三个圆
  63. turtle.pendown()
  64. turtle.fillcolor('yellow')
  65. turtle.begin_fill()
  66. turtle.pencolor('yellow')
  67. turtle.circle(10,360)
  68. turtle.end_fill()
  69. turtle.penup()
  70. turtle.goto(-214,-28)#左边第二个圆
  71. turtle.pendown()
  72. turtle.fillcolor('yellow')
  73. turtle.begin_fill()
  74. turtle.pencolor('yellow')
  75. turtle.circle(10,360)
  76. turtle.penup()
  77. turtle.goto(-230,-200)#左边第一个圆
  78. turtle.pendown()
  79. turtle.circle(10,360)
  80. turtle.end_fill()
  81. turtle.penup()
  82. turtle.goto(-5,325)#最上面的星星
  83. turtle.seth(80)
  84. turtle.pendown()
  85. turtle.fillcolor('tomato')
  86. turtle.begin_fill()
  87. turtle.pencolor('tomato')
  88. while True:
  89.     turtle.fd(50)
  90.     turtle.rt(144)
  91.     turtle.fd(50)
  92.     turtle.rt(144)
  93.     turtle.fd(50)
  94.     turtle.rt(144)
  95.     turtle.fd(50)
  96.     turtle.rt(144)
  97.     turtle.fd(50)
  98.     turtle.rt(144)
  99.     if abs(turtle.pos())<1:
  100.         break
  101. turtle.end_fill()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-11 11:42:54 | 显示全部楼层

最上面画笔还是会一直循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-11 11:44:24 | 显示全部楼层
巴巴鲁 发表于 2021-3-11 11:24
pos()改成turtle.pos()
这是turtle库的pos函数:abs(pos())

谢谢大佬,但是加了break还是会一直循环啊
66666.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-11 11:48:05 | 显示全部楼层
guhusf 发表于 2021-3-11 11:44
谢谢大佬,但是加了break还是会一直循环啊

我觉得挺好看的,动态效果
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-11 12:02:03 | 显示全部楼层    本楼为最佳答案   
guhusf 发表于 2021-3-11 11:42
最上面画笔还是会一直循环

一直不满足条件,不会跳出,这里类似于死循环,可以加个print(abs(turtle.pos()))来监控

  1. import turtle
  2. turtle.setup(800,800)
  3. turtle.pensize(5)
  4. turtle.pencolor('green')
  5. turtle.speed('fastest')
  6. turtle.penup()
  7. turtle.goto(50,-200)
  8. turtle.pendown()
  9. turtle.fillcolor('green')
  10. turtle.begin_fill()
  11. turtle.fd(200)
  12. turtle.seth(130)
  13. turtle.fd(220)
  14. turtle.rt(130)
  15. turtle.fd(120)
  16. turtle.seth(130)
  17. turtle.fd(200)
  18. turtle.rt(130)
  19. turtle.fd(100)
  20. turtle.seth(130)
  21. turtle.fd(300)
  22. turtle.lt(100)
  23. turtle.fd(300)
  24. turtle.lt(130)
  25. turtle.fd(100)
  26. turtle.rt(130)
  27. turtle.fd(200)
  28. turtle.lt(130)
  29. turtle.fd(120)
  30. turtle.rt(130)
  31. turtle.fd(220)
  32. turtle.lt(130)
  33. turtle.fd(200)
  34. turtle.rt(90)
  35. turtle.fd(100)
  36. turtle.lt(90)
  37. turtle.fd(85)
  38. turtle.lt(90)
  39. turtle.fd(100)
  40. turtle.end_fill()
  41. turtle.penup()
  42. turtle.goto(260,-200)#右下第一个圆
  43. turtle.pendown()
  44. turtle.fillcolor('yellow')
  45. turtle.begin_fill()
  46. turtle.pencolor('yellow')
  47. turtle.circle(10,360)
  48. turtle.penup()
  49. turtle.goto(244,-28)#右边第二个圆
  50. turtle.pendown()
  51. turtle.circle(10,360)
  52. turtle.end_fill()
  53. turtle.penup()
  54. turtle.goto(210,130)#右边第三个圆
  55. turtle.pendown()
  56. turtle.fillcolor('yellow')
  57. turtle.begin_fill()
  58. turtle.pencolor('yellow')
  59. turtle.circle(10,360)
  60. turtle.end_fill()
  61. turtle.penup()
  62. turtle.goto(-180,130)#左边第三个圆
  63. turtle.pendown()
  64. turtle.fillcolor('yellow')
  65. turtle.begin_fill()
  66. turtle.pencolor('yellow')
  67. turtle.circle(10,360)
  68. turtle.end_fill()
  69. turtle.penup()
  70. turtle.goto(-214,-28)#左边第二个圆
  71. turtle.pendown()
  72. turtle.fillcolor('yellow')
  73. turtle.begin_fill()
  74. turtle.pencolor('yellow')
  75. turtle.circle(10,360)
  76. turtle.penup()
  77. turtle.goto(-230,-200)#左边第一个圆
  78. turtle.pendown()
  79. turtle.circle(10,360)
  80. turtle.end_fill()
  81. turtle.penup()
  82. turtle.goto(-5,325)#最上面的星星
  83. turtle.seth(80)
  84. turtle.pendown()
  85. turtle.fillcolor('tomato')
  86. turtle.begin_fill()
  87. turtle.pencolor('tomato')
  88. while True:
  89.     turtle.fd(50)
  90.     turtle.rt(144)
  91.     turtle.fd(50)
  92.     turtle.rt(144)
  93.     turtle.fd(50)
  94.     turtle.rt(144)
  95.     turtle.fd(50)
  96.     turtle.rt(144)
  97.     turtle.fd(50)
  98.     turtle.rt(144)
  99.     print(abs(turtle.pos()))
  100.     if abs(turtle.pos())<1:
  101.         break
  102. turtle.end_fill()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-11 12:04:04 | 显示全部楼层
或者你可以修改跳出条件,闪十下就跳出循环

  1. import turtle
  2. turtle.setup(800,800)
  3. turtle.pensize(5)
  4. turtle.pencolor('green')
  5. turtle.speed('fastest')
  6. turtle.penup()
  7. turtle.goto(50,-200)
  8. turtle.pendown()
  9. turtle.fillcolor('green')
  10. turtle.begin_fill()
  11. turtle.fd(200)
  12. turtle.seth(130)
  13. turtle.fd(220)
  14. turtle.rt(130)
  15. turtle.fd(120)
  16. turtle.seth(130)
  17. turtle.fd(200)
  18. turtle.rt(130)
  19. turtle.fd(100)
  20. turtle.seth(130)
  21. turtle.fd(300)
  22. turtle.lt(100)
  23. turtle.fd(300)
  24. turtle.lt(130)
  25. turtle.fd(100)
  26. turtle.rt(130)
  27. turtle.fd(200)
  28. turtle.lt(130)
  29. turtle.fd(120)
  30. turtle.rt(130)
  31. turtle.fd(220)
  32. turtle.lt(130)
  33. turtle.fd(200)
  34. turtle.rt(90)
  35. turtle.fd(100)
  36. turtle.lt(90)
  37. turtle.fd(85)
  38. turtle.lt(90)
  39. turtle.fd(100)
  40. turtle.end_fill()
  41. turtle.penup()
  42. turtle.goto(260,-200)#右下第一个圆
  43. turtle.pendown()
  44. turtle.fillcolor('yellow')
  45. turtle.begin_fill()
  46. turtle.pencolor('yellow')
  47. turtle.circle(10,360)
  48. turtle.penup()
  49. turtle.goto(244,-28)#右边第二个圆
  50. turtle.pendown()
  51. turtle.circle(10,360)
  52. turtle.end_fill()
  53. turtle.penup()
  54. turtle.goto(210,130)#右边第三个圆
  55. turtle.pendown()
  56. turtle.fillcolor('yellow')
  57. turtle.begin_fill()
  58. turtle.pencolor('yellow')
  59. turtle.circle(10,360)
  60. turtle.end_fill()
  61. turtle.penup()
  62. turtle.goto(-180,130)#左边第三个圆
  63. turtle.pendown()
  64. turtle.fillcolor('yellow')
  65. turtle.begin_fill()
  66. turtle.pencolor('yellow')
  67. turtle.circle(10,360)
  68. turtle.end_fill()
  69. turtle.penup()
  70. turtle.goto(-214,-28)#左边第二个圆
  71. turtle.pendown()
  72. turtle.fillcolor('yellow')
  73. turtle.begin_fill()
  74. turtle.pencolor('yellow')
  75. turtle.circle(10,360)
  76. turtle.penup()
  77. turtle.goto(-230,-200)#左边第一个圆
  78. turtle.pendown()
  79. turtle.circle(10,360)
  80. turtle.end_fill()
  81. turtle.penup()
  82. turtle.goto(-5,325)#最上面的星星
  83. turtle.seth(80)
  84. turtle.pendown()
  85. turtle.fillcolor('tomato')
  86. turtle.begin_fill()
  87. turtle.pencolor('tomato')
  88. n = 10
  89. while n:
  90.     turtle.fd(50)
  91.     turtle.rt(144)
  92.     turtle.fd(50)
  93.     turtle.rt(144)
  94.     turtle.fd(50)
  95.     turtle.rt(144)
  96.     turtle.fd(50)
  97.     turtle.rt(144)
  98.     turtle.fd(50)
  99.     turtle.rt(144)
  100.     n-=1
  101. turtle.end_fill()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 07:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表