Python 如何从用户那里获得输入,并让海龟画出来?

Python 如何从用户那里获得输入,并让海龟画出来?,python,input,console,turtle-graphics,Python,Input,Console,Turtle Graphics,我想要一只乌龟来绘制用户在控制台中输入的内容。例如:如果用户输入“CLOCK”,我希望乌龟以所有大写字母绘制“CLOCK” 我制作了几乎所有的字母+空格和句号,并用名称“letterA”,“letterB”等来定义它们 我已经寻找了很长一段时间的方法来做到这一点,但我想不出来 我的海龟的名字是“t”,变量是turtleEnd/Start/MidSpot。如果用户输入的任何字符不是英文字母+Æ、Ø、Å,则控制台输出如下内容: “句子中的字符无效,请重试” 代码:忽略print语句,它们只用于我的日

我想要一只乌龟来绘制用户在控制台中输入的内容。例如:如果用户输入
“CLOCK”
,我希望乌龟以所有大写字母绘制
“CLOCK

我制作了几乎所有的字母+空格和句号,并用名称
“letterA”
“letterB”
等来定义它们

我已经寻找了很长一段时间的方法来做到这一点,但我想不出来

我的海龟的名字是
“t”
,变量是
turtleEnd/Start/MidSpot
。如果用户输入的任何字符不是英文字母
+Æ、Ø、Å
,则控制台输出如下内容:

“句子中的字符无效,请重试”

代码:忽略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')

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

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

这显然并不完整,但它展示了将函数存储在字典中绘制字母,并在循环输入时检索(并调用)字母的想法。您可能想考虑如何处理字符之间的空格。 您的问题是如何处理尚未创建版本的字符吗?这是相当不清楚的。包括您的代码并明确您的具体要求。尝试Unicode:
t.write(u“Æ,Ø,Ø”)
。如果你能分享你的代码,我会有所帮助。@Carcigenicate现在我已经把我的code@Hapalop现在我已经包含了我的代码如果我删除了
try:
除了keyrerror:
部分,那么它只适用于短语中的字母
:字母[letter]()
phrase = input('enter phrase: ')
for letter in phrase:
    try:
        letters[letter]()
    except KeyError:
        raise TypeError('invalid character {}'
                        .format(letter)) from None