简体   繁体   中英

Using turtle to draw inputs in Python

I am working on a final project for python for my intro to python class. I have written code for each of the letters in the alphabet, my idea is to have the user input some words, and return code for all of the input letters. I have tried using a = code for turtle but this does not work. Any Ideas?

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

print(codes[str(x)])

Would a dictionary with an input() work?

Let's expand (and correct) @GerardAnthonyMcBride's dictionary-based approach. Here's an overly simplified example that just prints the letters 'S' and '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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM