我正在研究python的最终项目,我的python类简介。我已经为字母表中的每个字母编写了代码,我的想法是让用户输入一些单词,并返回所有输入字母的代码。我尝试使用= 代码来实现海龟,但这不起作用。任何想法?
答案 0 :(得分:0)
x = input()
codes = {'a': 'code for turtle',.....}
print(codes[str(x)])
带有input()的字典是否有效?
答案 1 :(得分: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()
<强>输出强>