如何从用户那里获得输入,并让乌龟绘制它?

时间:2018-11-12 19:08:09

标签: python input console turtle-graphics

我想要一个乌龟,它可以绘制用户在控制台中输入的内容。例如:如果用户输入"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')

1 个答案:

答案 0 :(得分:0)

如果您定义了绘制字母的功能,请使用以下思路:

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

这显然还没有完成,但是它显示了这样的想法:存储您的函数以在字典中绘制字母并在您遍历输入时检索它们(并调用它们)。您可能需要考虑如何处理字符之间的空格。