首页 > 解决方案 > 如何从用户那里获取输入,并让乌龟绘制它?

问题描述

我想要一个海龟来绘制用户在控制台中输入的内容。例如:如果用户输入"CLOCK",我希望海龟绘制"CLOCK“,全部大写。

我已经制作了几乎所有的字母+空格和句点,并用名称定义它们"letterA""letterB"等。

我已经寻找了很长时间的方法来做到这一点,但我无法弄清楚。

我的乌龟的名字是"t",我的变量是turtleEnd/Start/MidSpot。如果用户输入了任何不在英文字母表中的字符+ Æ,Ø,Å,那么控制台会输出如下内容:

"Invalid character in sentence, please try again"

代码:忽略print语句,它们仅用于我的日志记录

    import turtle as t

turtleEndSpot = t.position()
turtleStartSpot = t.position()
turtleMidSpot = t.position()

#------------------------ALLE BOKSTAVENE OG TEGNENE--------------------
def letterA():
    print('\x1b[1;30;41m' + 'STARTER letterA' + '\x1b[0m')
    t.pendown() 
    t.left(78) 
    t.forward(110)
    t.right(156)
    t.forward(110)
    turtleEndSpot = t.position()
    t.back(25)
    t.right(102)
    t.forward(37)
    t.penup()
    t.setpos(turtleEndSpot)
    t.setheading(0)
    t.forward(20)
    print('\x1b[0;30;42m' + 'letterA FERDIG!' + '\x1b[0m')
def letterB():
    print('\x1b[1;30;41m' + 'STARTER letterB' + '\x1b[0m')
    t.pendown()
    t.left(90)
    t.forward(107.59)
    t.right(90)
    for i in range(7):
        t.forward(14)
        t.right(30)
    t.setheading(0)
    for i in range(7):
        t.forward(15)
        t.right(30)
    t.setheading(0)
    t.penup()
    t.forward(45)
    print('\x1b[0;30;42m' + 'letterB FERDIG!' + '\x1b[0m')
def letterC():
    print('\x1b[1;30;41m' + 'STARTER letterC' + '\x1b[0m')
    t.penup() 
    turtleStartSpot = t.position() 
    t.forward(5) 
    t.setheading(90) 
    for i in range(7):
        t.forward(14)
        t.right(30)
    t.setheading(-90)
    t.backward(75)
    t.pendown()
    for i in range(7):
        t.backward(14)
        t.left(30)
    t.setheading(-90)
    t.forward(41)
    for i in range(7):
        t.forward(14)
        t.left(30)
    t.penup()
    t.setpos(turtleStartSpot)
    t.setheading(0)
    t.forward(75)
    print('\x1b[0;30;42m' + 'letterC FERDIG!' + '\x1b[0m')
def letterD():
    print('\x1b[1;30;41m' + 'STARTER letterD' + '\x1b[0m')
    t.pendown()
    t.setheading(90)
    t.forward(108)
    t.setheading(0)
    for i in range(4):
        t.forward(20)
        t.right(30)
    t.setheading(-90)
    t.forward(13)
    for i in range(4):
        t.forward(20)
        t.right(30)
    t.setheading(0)
    t.penup()
    t.forward(65)
    print('\x1b[0;30;42m' + 'letterD FERDIG!' + '\x1b[0m')
def letterE():
    print('\x1b[1;30;41m' + 'STARTER letterE' + '\x1b[0m')
    t.pendown()
    turtleStartSpot=t.position()
    t.setheading(90)
    t.forward(54)
    turtleMidSpot = t.position()
    t.forward(54)
    t.setheading(0)
    t.forward(40)
    t.penup()
    t.setpos(turtleMidSpot)
    t.pendown()
    t.forward(35)
    t.penup()
    t.setpos(turtleStartSpot) 
    t.pendown()
    t.forward(40)
    t.penup()
    t.forward(15)
    print('\x1b[0;30;42m' + 'letterE FERDIG!' + '\x1b[0m')
def letterF():
    print('\x1b[1;30;41m' + 'STARTER letterF' + '\x1b[0m')
    t.pendown()
    turtleStartSpot=t.position()
    t.setheading(90)
    t.forward(54)
    turtleMidSpot = t.position()
    t.forward(54)
    t.setheading(0)
    t.forward(40)
    t.penup()
    t.setpos(turtleMidSpot)
    t.pendown()
    t.forward(35)
    t.penup()
    t.setpos(turtleStartSpot) 
    t.forward(40)
    t.penup()
    t.forward(15)
    print('\x1b[0;30;42m' + 'letterF FERDIG!' + '\x1b[0m')
def letterG():
    print('\x1b[1;30;41m' + 'STARTER letterG' + '\x1b[0m')
    t.penup() 
    turtleStartSpot = t.position() 
    t.forward(5) 
    t.setheading(90) 
    for i in range(7):
        t.forward(14)
        t.right(30)
    t.setheading(-90)
    t.backward(75)
    t.pendown()
    for i in range(7):
        t.backward(14)
        t.left(30)
    t.setheading(-90)
    t.forward(41)
    for i in range(7):
        t.forward(14)
        t.left(30)
    t.setheading(180)
    t.forward(20)
    t.penup()
    t.setpos(turtleStartSpot)
    t.setheading(0)
    t.forward(75)
    print('\x1b[0;30;42m' + 'letterG FERDIG!' + '\x1b[0m')
def letterH():
    print('\x1b[1;30;41m' + 'STARTER letterH' + '\x1b[0m')
    t.pendown()
    t.setheading(90)
    t.forward(107.59)
    t.backward(54)
    t.setheading(0)
    t.forward(35)
    t.setheading(90)
    t.forward(54)
    t.backward(107.59)
    t.setheading(0)
    t.penup()
    t.forward(20)
    print('\x1b[0;30;42m' + 'letterH FERDIG!' + '\x1b[0m')
def letterI():
    print('\x1b[1;30;41m' + 'STARTER letterI' + '\x1b[0m')
    t.pendown()
    t.setheading(90)
    t.forward(107.59)
    t.backward(107.59)
    t.setheading(0)
    t.penup()
    t.forward(20)
    print('\x1b[0;30;42m' + 'letterI FERDIG!' + '\x1b[0m')
def letterJ():
    print('\x1b[1;30;41m' + 'STARTER letterJ' + '\x1b[0m')
    t.forward(50)
    t.setheading(90)
    t.forward(107.59)
    t.pendown()
    t.setheading(-90)
    t.forward(74)
    turtleMidSpot = t.position()
    for i in range(7):
        t.forward(14)
        t.right(30)
    t.penup()
    t.setpos(turtleMidSpot)
    t.setheading(-90)
    t.forward(30)
    t.setheading(0)
    t.forward(20)
    print('\x1b[0;30;42m' + 'letterJ FERDIG!' + '\x1b[0m')
def letterK():
    print('\x1b[1;30;41m' + 'STARTER letterK' + '\x1b[0m')
    t.setheading(90)
    t.pendown()
    t.forward(54)
    turtleMidSpot=t.position()
    t.forward(53)
    t.setpos(turtleMidSpot)
    t.setheading(57)
    t.forward(15)
    turtleMidSpot=t.position()
    t.forward(45)
    t.setpos(turtleMidSpot)
    t.setheading(-60)
    t.forward(79)
    t.setheading(0)
    t.penup()
    t.forward(15)
    print('\x1b[0;30;42m' + 'letterK FERDIG!' + '\x1b[0m')
def letterL():
    print('\x1b[1;30;41m' + 'STARTER letterL' + '\x1b[0m')
    t.pendown()
    turtleStartSpot = t.position()
    t.setheading(90)
    t.forward(107.59)
    t.setpos(turtleStartSpot)
    t.setheading(0)
    t.forward(30)
    t.penup()
    t.forward(20)
    print('\x1b[0;30;42m' + 'letterL FERDIG!' + '\x1b[0m')
def letterM():
    print('\x1b[1;30;41m' + 'STARTER letterM' + '\x1b[0m')
    t.pendown()
    t.setheading(90)
    t.forward(107.59)
    t.setheading(-60)
    t.forward(50)
    t.left(120)
    t.forward(50)
    t.setheading(-90)
    t.forward(107.59)
    t.setheading(0)
    t.penup()
    t.forward(20)
    print('\x1b[0;30;42m' + 'letterM FERDIG!' + '\x1b[0m')
def letterN():
    print('\x1b[1;30;41m' + 'STARTER letterN' + '\x1b[0m')
    t.setheading(90)
    t.pendown()
    t.forward(107.59)
    t.setheading(-70)
    t.forward(115)
    t.setheading(90)
    turtleEndSpot = t.position()
    t.forward(107.59)
    t.penup()
    t.setpos(turtleEndSpot)
    t.setheading(0)
    t.forward(20)
    print('\x1b[0;30;42m' + 'letterL FERDIG!' + '\x1b[0m')
def letterO():
    print('\x1b[1;30;41m' + 'STARTER letterO' + '\x1b[0m')
    t.penup() 
    turtleStartSpot = t.position() 
    t.forward(5) 
    t.setheading(90) 
    for i in range(7):
        t.forward(14)
        t.right(30)
    t.setheading(-90)
    t.backward(75)
    t.pendown()
    for i in range(7):
        t.backward(14)
        t.left(30)
    t.setheading(-90)
    t.forward(41)
    for i in range(7):
        t.forward(14)
        t.left(30)
    t.setheading(90)
    t.forward(50)
    t.penup()
    t.setpos(turtleStartSpot)
    t.setheading(0)
    t.forward(75)
    print('\x1b[0;30;42m' + 'letterO FERDIG!' + '\x1b[0m')
def letterP():
    print('\x1b[1;30;41m' + 'STARTER letterP' + '\x1b[0m')
    t.pendown()
    turtleStartSpot = t.position()
    t.left(90)
    t.forward(107.59)
    t.right(90)
    for i in range(7):
        t.forward(15)
        t.right(30)
    t.penup()
    t.setpos(turtleStartSpot)
    t.setheading(0)
    t.forward(40)
    print('\x1b[0;30;42m' + 'letterP FERDIG!' + '\x1b[0m')
def letterQ():
    print('\x1b[1;30;41m' + 'STARTER letterQ' + '\x1b[0m')
    t.penup() 
    turtleStartSpot = t.position() 
    t.forward(5) 
    t.setheading(90) 
    for i in range(7):
        t.forward(14)
        t.right(30)
    t.setheading(-90)
    t.backward(75)
    t.pendown()
    for i in range(7):
        t.backward(14)
        t.left(30)
    t.setheading(-90)
    t.forward(41)
    for i in range(7):
        t.forward(14)
        t.left(30)
    t.setheading(90)
    t.forward(50)
    t.penup()
    t.setpos(turtleStartSpot)
    t.setheading(0)
    t.forward(50)
    t.setheading(120)
    t.turtleEndSpot = t.position()
    t.pendown()
    t.forward(30)
    t.penup()
    t.setpos(turtleEndSpot)
    t.setheading(0)
    t.forward(20)
    print('\x1b[0;30;42m' + 'letterO FERDIG!' + '\x1b[0m')
def letterR():
    print('\x1b[1;30;41m' + 'STARTER letterR' + '\x1b[0m')
    t.pendown()
    t.left(90)
    t.forward(107.59)
    t.right(90)
    for i in range(7):
        t.forward(14)
        t.right(30)
    t.setheading(-58)
    t.forward(68)
    t.setheading(0)
    t.penup()
    t.forward(45)
    print('\x1b[0;30;42m' + 'letterR FERDIG!' + '\x1b[0m')
def letterS():
    print('\x1b[1;30;41m' + 'STARTER letterS' + '\x1b[0m')
    t.penup() 
    turtleStartSpot = t.position() 
    t.backward(20) 
    t.setheading(90) 
    for i in range(7):
        t.forward(14)
        t.right(30)
    t.setheading(-90)
    t.backward(75)
    t.pendown()
    for i in range(8):
        t.backward(14)
        t.left(30)
    t.setheading(-30)
    t.forward(40)
    t.setheading(-60)
    for i in range(8):
        t.forward(13)
        t.right(30)
    t.penup()
    t.setpos(turtleStartSpot)
    t.setheading(0)
    t.forward(60)
    print('\x1b[0;30;42m' + 'letterS FERDIG!' + '\x1b[0m')
def letterT():
    print('\x1b[1;30;41m' + 'STARTER letterT' + '\x1b[0m')
    t.penup()
    t.forward(30)
    turtleEndSpot = t.position()
    t.setheading(90)
    t.pendown()
    t.forward(107.59)
    t.setheading(180)
    t.forward(30)
    t.backward(60)
    t.penup()
    t.setpos(turtleEndSpot)
    t.setheading(0)
    t.forward(60)
    print('\x1b[0;30;42m' + 'letterT FERDIG!' + '\x1b[0m')
def letterU():
def specialSpace():
    t.penup()
    t.setheading(0)
    t.forward(40)
def specialPeriod():
    t.pendown()
    t.dot(5)
    t.penup()
    specialSpace()
#-----------------------------------------------------------------------
def lettersInTheAlphabet():
    letterA()
    print(" ")
    letterB()
    print(" ")
    letterC()
    print(" ")
    letterD()
    print(" ")
    letterE()
    print(" ")
    letterF()
    print(" ")
    letterG()
    print(" ")
    letterH()
    print(" ")
    letterI()
    print(" ")
    letterJ()
    print(" ")
    letterK()
    print(" ")
    letterL()
    print(" ")
    letterM()
    print(" ")
    letterN()
    print(" ")
    letterO()
    print(" ")
    letterP()
    print(" ")
    letterQ()
    print(" ")
    letterR()
    print(" ")
    letterS()
    print(" ")
    letterT()
    print(" ")
    print("Alfabet ferdig\n---------------")
def readyCanvas():
    t.pensize(5)
    t.shape("turtle")
    t.speed(1000000000)
    t.setpos(-1000,0)
    t.color("magenta")
    t.forward(3000)
    t.setpos(-1000,0)
    t.setheading(90)
    t.forward(107.59)
    t.setheading(0)
    t.forward(3000)
    t.pu()
    t.setpos(-1000,0)
    t.color("black")
    t.pensize(2)

readyCanvas()
lettersInTheAlphabet()

print(" ")
print('\x1b[5;31;41m' + 'PROGRAM FERDIG!' + '\x1b[0m')

标签: pythoninputconsoleturtle-graphics

解决方案


如果您已经定义了绘制字母的函数,请使用以下想法:

letters = {
    'A': letterA,   # <— that’s a function
    'B': letterB,
    ...
}

然后遍历用户输入并尝试绘制:

phrase = input('enter phrase: ')
for letter in phrase:
    try:
        letters[letter]()
    except KeyError:
        raise TypeError('invalid character {}'
                        .format(letter)) from None

这显然不完整,但它显示了存储函数以在字典中绘制字母并在循环输入时检索它们(并调用它们)的想法。您可能需要考虑如何处理在字符之间放置空格。


推荐阅读