[英]Using turtle to draw inputs in Python
我正在為python類簡介進行python的最終項目。 我已經為字母表中的每個字母編寫了代碼,我的想法是讓用戶輸入一些單詞,並為所有輸入的字母返回代碼。 我嘗試對烏龜使用= 代碼,但這不起作用。 有任何想法嗎?
x = input()
codes = {'a': 'code for turtle',.....}
print(codes[str(x)])
帶有input()的字典可以工作嗎?
讓我們擴展(並更正)@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()
OUTPUT
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.