我想製作一個程序,根據用戶輸入繪製某些行。類型錯誤與執行()
#import turtle program
#called it cassy cause I was trying to come up with a fun name for an arrow
that they call 'classic'. I tried ok.
import turtle
cassy = turtle.Turtle()
cassy.shape('classic')
#set's the window shape, so I can give myself more room
#for reference, putting startx and y as none centers it
turtle.setup(width=1200, height=600, startx=None, starty=None)
#used to find the height of what the screen size is so I could figure out
what it was set at.
"""print(turtle.window_width())
print(turtle.window_height())"""
#gives our wonderful circle a home (where it starts), to the right and
middle of the box
#old code (turtle.setworldcoordinates(-20, -20, 0, 20))
cassy.hideturtle()
cassy.penup()
cassy.goto(580, 0)
#sets the turtle to be facing left
cassy.left(180)
cassy.showturtle()
cassy.pendown()
#as always, I gotta have that sweet sweet starting message.
print("Welcome to the line generator!")
print("To start, type in a letter")
#using a dictionary function so I can assign each character a "Line"
line = {
"a" : "cassy.left(20)",
"b" : "cassy.forward(20)",
"c" : "cassy.right(45)",
"d" : "cassy.right(70)",
"e" : "cassy.circle(10)",
"f" : "cassy.left(15)",
"g" : "cassy.forward(15)",
"h" : "cassy.left(60)",
"i" : "cassy.forward(10)",
"j" : "cassy.forward(55)",
"k" : "cassy.circle(50, 100)",
"l" : "cassy.right(80)",
"m" : "cassy.forward(35)",
"n" : "cassy.left(75)",
"o" : "cassy.forward(45)",
"p" : "cassy.right(5)",
"q" : "cassy.forward(100)",
"r" : "cassy.right(65)",
"s" : "cassy.forward(125)",
"t" : "cassy.left(25)",
"u" : "cassy.forward(150)",
"v" : "cassy.right(95)",
"w" : "cassy.forward(120)",
"x" : "cassy.circle(25, 175)",
"y" : "cassy.circle(5, 190)",
"z" : "cassy.forward(175)",
"0" : "cassy.right(20)",
"1" : "cassy.forward(210)",
"2" : "cassy.left(120)",
"3" : "cassy.forward(115)",
"4" : "cassy.forward(15)",
"5" : "cassy.circle(30, 150)",
"6" : "cassy.left(70)",
"7" : "cassy.right(40)",
"8" : "cassy.backward(30)",
"9" : "cassy.backward(130)"
}
#let's the user type in letters/numbers (and only those (unless I want to
mix things up; we'll see))
while True:
draw = input("Type anything to draw! ")
#if statement to make sure that they only type in numbers or letters
if(draw.isalnum() == True):
#I can only assume that this executes the code inside the quotes
#but I haven't looked too deeply into what exec() does
exec(line.get(draw))
#this seperates each character drawn by a space, for funsies (yes I know it's a weird thing to say)
cassy.penup()
cassy.forward(15)
cassy.pendown()
#else statement in case they type in other charaters that aren't numbers or letters
else:
print("Please only use letters or numbers.")
好了,所以我知道這是一個很多的閱讀,但我真正的問題是這行代碼
exec(line.get(draw))
到目前爲止,當我在1個字母同時鍵入程序纔有效。但如果我嘗試並鍵入多個字母,比如那句「你好」它給了我這個錯誤
Traceback (most recent call last):
File "C:/Users/73059/Final Project/Final Project funstuffs.py", line 87, in <module>
exec(line.get(draw))
TypeError: exec() arg 1 must be a string, bytes or code object
我真的不知道了很多關於函數的exec(),我已經一直在試圖看看它的東西,這是一個項目,我真的沒有太多時間。
如果有任何問題,或者你不明白我想完成什麼,請告訴我。我有點絕望。
感謝
感謝您的反饋和幫助,我的項目現在運行順利。 – Benita