2017-02-10 101 views
0

只是審查我即將到來的期中。我們被給予過去的中期問題,但沒有解決方案。我正努力把握最好的知識。Python龜圖形簡化

對於這個問題,它要求定義一個名爲equalSigns的函數,傳遞它的值t和長度。所以,我只需要在烏龜圖形中製作我的程序,創建兩條parellel線,我認爲很簡單。這是我寫的代碼,只是爲了正確輸出x長度的等號。 (當然我會把它轉換成函數)我的問題,有沒有更好的方法來創建這個?

import turtle 
t=turtle.Turtle() 
s=turtle.Screen() 

t.forward(200) 
t.penup() 
t.home() 
t.right(90) 
t.forward(50) 
t.pendown() 
t.left(90) 
t.forward(200) 
'''i suppose i dont have to go home and then down. 
instead just continue and go down and forward left. 
but either way, is this the best approach to take? 
''' 

回答

2

是的,我認爲還有更好的辦法。最重要的是,我認爲你的方向是錯誤的:你需要第二個右轉回到下一行。

可能做一個例程,做一半等於,然後所有它兩次得到兩條線。把它想象成一個矩形,除了短邊是不可見的。

# Draw long side 
t.pendown() 
t.forward(x) 
t.penup() 
t.right(90) 

# Move along short side without drawing 
t.forward(x/4) 
t.right(90) 

這會讓您到達矩形的對角。請撥打這個電話兩次,然後您就完成了...並返回到起點。

+0

我出來很好,只是顯得有點不正統,謝謝你我這是我在找什麼! – Ryan

0

也許你可以有你的龜認爲殼外:

import turtle 
import tkinter as _ 

_.ROUND = _.BUTT 

turtle.width(50) 
turtle.forward(200) 
turtle.color("white") 
turtle.width(48) 
turtle.backward(200) 

turtle.done() 

enter image description here

(垂直灰色在兩端酒吧GIF轉換的文物和在程序運行時不存在。 )