2017-08-15 48 views
0

我正在研究python的最後一個項目,用於我的Python入門介紹。我已經爲字母表中的每個字母編寫了代碼,我的想法是讓用戶輸入一些字詞,並返回所有輸入字母的代碼。我已經嘗試使用= 代碼爲龜,但這不起作用。有任何想法嗎?使用龜來在Python中繪製輸入

回答

0
x = input() 
codes = {'a': 'code for turtle',.....} 

print(codes[str(x)]) 

將輸入()工作的字典?

+1

嗯可能讓我給它一個鏡頭 – physicslifter

+1

酷豆!讓我知道事情的後續! – GerryMcBride

+0

好吧,因爲它會讀取烏龜而產生錯誤,併產生語法錯誤,並將導入烏龜置於頂部並不能解決此問題。 – physicslifter

0

讓我們來擴展(並更正)@GerardAnthonyMcBride的基於字典的方法。下面是一個過於簡單的例子,只是打印的字母「S」和「O」:

from turtle import Turtle, Screen 

SIZE = 100 

def draw_O(turtle): 
    turtle.pendown() 
    for _ in range(4): 
     turtle.forward(SIZE) 
     turtle.left(90) 
    turtle.penup() 

def draw_S(turtle): 
    position = turtle.position() 
    turtle.pendown() 

    turtle.forward(SIZE) 
    turtle.left(90) 
    turtle.forward(SIZE/2) 
    turtle.left(90) 
    turtle.forward(SIZE) 
    turtle.right(90) 
    turtle.forward(SIZE/2) 
    turtle.right(90) 
    turtle.forward(SIZE) 

    # leave turtle as we found it 
    turtle.penup() 
    turtle.setposition(position) 

characters = { 
    'O': draw_O, 
    'S': draw_S, 
    } 

screen = Screen() 
yertle = Turtle() 

string = input() 

for character in string: 
    if character in characters: 
     characters[character](yertle) 
    yertle.forward(SIZE * 1.25) 

screen.exitonclick() 

輸出

enter image description here