python 中的 eval 函数未实现错误
Not implemented error with eval function in python
我正在用 python 乌龟做点什么。下面显示的 eval 函数应该是 运行 h() 或 i()。这些是目前唯一可用的功能,因此唯一可用的是 hi 或 ih 或 i 或 h。控制台returns一个notimplementederror: eval is not yet implemented。我不明白,因为我进入了一个全新的事物,并输入了像 eval('1') 这样的基本代码,但这也不起作用。谢谢 顺便说一下,我是 python 的新手,所以我不擅长,所以如果我犯了愚蠢的错误,请注意我。
import turtle
from time import sleep
ninja = turtle.Turtle()
ninja.hideturtle()
coordinate1 = ninja.xcor()
coordinate2 = ninja.ycor()
new1=''
new2=''
ninja.speed(10)
def h():
ninja.left(90)
ninja.forward(50)
ninja.back(100)
ninja.forward(50)
ninja.right(90)
ninja.forward(35)
ninja.left(90)
ninja.forward(-50)
ninja.forward(100)
coordinate1 = ninja.xcor()
coordinate2 = ninja.ycor()
new1 = coordinate1+50
ninja.penup()
ninja.goto(new1,0)
def i():
ninja.forward(20)
ninja.pendown()
ninja.left(90)
ninja.st()
ninja.right(90)
ninja.stamp()
ninja.ht()
ninja.penup()
ninja.back(20)
ninja.pendown()
ninja.back(50)
coordinate1 = ninja.xcor()
new1 = coordinate1+50
ninja.penup()
ninja.goto(new1,0)
h()
i()
name = input('What is your name. It will be drawn in the tab to the left lowercase only please.')
print('The name will begin to draw in the tab to the left')
sleep(3)
ninja.clear()
ninja.goto(0,0)
name = list(name)
print(name)
length = len(name)
x=0
while (x < length-1):
print(name[x])
x = x + 1
new2=name[x]+'()'
print(new2)
eval(new2)
您不需要也不希望 eval()
实现此程序 -- 您可以使用将字符映射到函数的字典来实现。下面是使用字典而不是 eval()
:
的代码的简化返工
import turtle
def h():
ninja.pendown()
ninja.left(90)
ninja.forward(100)
ninja.backward(50)
ninja.right(90)
ninja.forward(40)
ninja.left(90)
ninja.forward(50)
ninja.backward(100)
ninja.right(90)
ninja.penup()
ninja.forward(25)
def i():
ninja.forward(20)
ninja.left(90)
ninja.forward(70)
ninja.right(90)
ninja.pendown()
ninja.circle(5)
ninja.penup()
ninja.left(90)
ninja.backward(20)
ninja.pendown()
ninja.backward(50)
ninja.right(90)
ninja.penup()
ninja.forward(45)
letters_to_code = {"h": h, "i": i}
name = input('What is your name? Lowercase only please: ')
ninja = turtle.Turtle()
ninja.penup()
for letter in name:
if letter in letters_to_code:
letters_to_code[letter]()
ninja.hideturtle()
turtle.done()
一些需要思考的事情:
具有标准字母高度、宽度和间距距离并将它们定义为您在实现字母时可以使用的变量。
为了下一个字母,让每个字母在海龟完成时保持其原始起始方向。这将允许您以任何顺序组合字母而不会感到意外。
如果您使用的是 trinket,最好的办法是绘制出从 a 到 z 的所有字母函数,并使用 if 语句指定要打印的字母以及要打印到屏幕上的最大字母数,具体取决于如何你把每个字母都画大了。这是我第一次看到饰品,我必须承认它既令人愉快又令人沮丧。
我以为我们都被卡在了 Cache-22 中,因为 trinket。io/python 不接受 eval 或全局变量,但是 repl.it 接受使用 eval 和全局变量,但是 repl.it 没有海龟屏
“cdlane 来拯救世界”
感谢 cdlane 的回答,我知道了一种不使用 globals() 的方法。
我用 eval、exec 和 globals() 尝试了所有不同的方法,但是 Interactive Python 3 Trinket online 不接受任何这些方法。
如果输入确切的名称,代码将从函数中打印出 momo 或 AstroBoy。这些是唯一支持的字母。如果这些字母中的任何一个以不同的顺序输入,结果可能有点不可预测,但仍然令人愉快。输入“mo”、“momo”、“mom”、“astro”、“astroboy”、“boy”、“bboy”、“oboy”、“yo boy”、“robot”等进行测试
checkName(name) 函数和 if 语句的编写是为了激励任何人绘制函数字母表,现在使用已知的解决方法来解决无法使用 globals() 的问题。 (通过使用长 if-else 怪物,或使用 cdlane 的天才答案)
虽然探索 Trinket 的乐趣很有趣,但我可能不会很快再玩 trinket,或者我可能会在遥远的未来某个时候回来玩 Trinket。如果我这样做,我不会在 Trinket 上花费太多时间,因为我开始了解 Pygame,因此 Pygame 是我的下一个预定体验。
cdlane 获得赞成票!
尽情享受吧!
在 https://trinket.io/python
测试
# Tested at https://trinket.io/python
# Written by AstroBoy R (featuring mOmO)
# Also featuring cdlane's workaround instead of using globals
import turtle
from time import sleep
ninja = turtle.Turtle()
ninja.hideturtle()
coordinate1 = ninja.xcor()
coordinate2 = ninja.ycor()
new1=''
new2=''
ninja.speed(10)
def h():
ninja.left(90)
ninja.forward(50)
ninja.back(100)
ninja.forward(50)
ninja.right(90)
ninja.forward(35)
ninja.left(90)
ninja.forward(-50)
ninja.forward(100)
coordinate1 = ninja.xcor()
coordinate2 = ninja.ycor()
new1 = coordinate1+50
ninja.penup()
ninja.goto(new1,0)
def i():
ninja.forward(20)
ninja.pendown()
ninja.left(90)
ninja.st()
ninja.right(90)
ninja.stamp()
ninja.ht()
ninja.penup()
ninja.back(20)
ninja.pendown()
ninja.back(50)
coordinate1 = ninja.xcor()
new1 = coordinate1+50
ninja.penup()
ninja.goto(new1,0)
h()
i()
name = input('What is your name. It will be drawn in the tab to the left lowercase only please.')
print('The name will begin to draw in the tab to the left')
sleep(3)
#ninja.clear()
ninja.goto(0,0)
print(name)
# "AstroBoy's Turtle Program (mOmO remix)"
from turtle import *
def m():
pendown()
begin_fill()
left(90)
forward(75)
right(90)
forward(30)
print(pos())
right(70)
forward(40)
print(pos())
left(140)
forward(40)
right(70)
forward(30)
right(90)
forward(75)
right(90)
forward(30)
right(90)
forward(40)
left(160)
forward(40)
right(135)
forward(40)
lt(155)
fd(40)
rt(90)
fd(29)
end_fill()
penup()
right(180)
forward(105)
print(pos())
def O():
fd(35)
pendown()
begin_fill()
circle(40)
end_fill()
pu()
left(90)
fd(30)
right(90)
pd()
begin_fill()
fillcolor('white')
circle(10)
end_fill()
pu()
right(90)
fd(30)
left(90)
fd(50)
print(pos())
def a():
pd()
lt(70)
fd(50)
rt(140)
fd(50)
pu()
lt(180)
fd(20)
lt(70)
pd()
fd(22)
pu()
lt(90)
fd(20)
lt(90)
fd(30)
def s():
fd(20)
pd()
circle(12,-90)
pu()
circle(12,90)
pd()
circle(12,90)
circle(12,90)
circle(-12,270)
pu()
fd(36)
lt(90)
fd(20)
def t():
fd(5)
pd()
lt(90)
fd(46)
lt(90)
fd(20)
rt(180)
fd(40)
rt(90)
pu()
fd(45)
lt(90)
fd(10)
def r():
pd()
lt(90)
fd(45)
rt(90)
fd(15)
if name=='robot':
begin_fill()
fillcolor('teal')
circle(-13,180)
fd(15)
rt(180)
fd(10)
pd()
if name=='robot':
end_fill()
rt(45)
fd(28)
lt(45)
pu()
fd(18)
def o():
fd(10)
pd()
circle(15,90)
fd(15)
circle(15,90)
fd(5)
circle(15,90)
fd(15)
circle(15,90)
fd(5)
pu()
fd(25)
print(pos())
def b():
fd(10)
lt(90)
pd()
begin_fill()
fillcolor('yellow')
fd(45)
rt(90)
fd(20)
circle(-11,180)
fd(10)
rt(180)
fd(10)
circle(-12,180)
fd(21)
end_fill()
pu()
lt(180)
fd(47)
def y():
fd(5)
pd()
begin_fill()
fillcolor('green')
fd(5)
pencolor('green')
lt(90)
pencolor('black')
fd(45)
lt(90)
pencolor('teal')
fd(20)
lt(135)
fd(22)
rt(45)
end_fill()
begin_fill()
fillcolor('chartreuse')
pencolor('dark green')
fd(29)
lt(90)
fd(9)
end_fill()
begin_fill()
pencolor('black')
lt(90)
fillcolor('lime')
fd(29)
rt(45)
fd(22)
lt(135)
fd(20)
end_fill()
pu()
rt(180)
fd(30)
rt(90)
fd(45)
lt(90)
def testingColorfill():
home()
pd()
c=['green','red','orange','blue', 'yellow', 'magenta', 'gold', 'pink', 'lime', 'cyan']
n=100
nc=0
while n >= 10:
begin_fill()
fillcolor(c[nc])
circle(n)
end_fill()
n = n-10
nc += 1
def testingmOmO():
m()
#Second letter color
fillcolor('yellow')
O()
#Third letter color
fillcolor('dodger blue')
m()
#Fourthletter color
fillcolor('green')
O()
testingColorfill()
testingAstroBoy()
def testingAstroBoy():
penup()
goto(-175, 0)
goto(-175, -80)
a()
s()
t()
r()
o()
b()
#color second letter o teal
begin_fill()
fillcolor('teal')
o()
end_fill()
y()
#Go to first letter position
penup()
goto(-170, 0)
goto(-170, -160)
fillcolor('crimson')
#testingmOmO()
import string
lowerc = string.ascii_lowercase
upperc = string.ascii_uppercase
momo = 'momo'
name = name.lower()
def checkmOmO(name):
if name == momo or len(name)==4:
testingmOmO()
wo=True
def checkName(name):
if len(name) > 8:
print "Sorry name is too long!"
return None
elif 'm' in name and len(name)==4:
checkmOmO(name)
return
else:
for n in name:
for i in range(len(lowerc)):
if i==0 and (n==lowerc[0]):
a()
elif i==1 and (n==lowerc[1]):
b()
elif i==2 and (n==lowerc[2]):
pass
elif i==3 and (n==lowerc[3]):
pass
elif i==4 and (n==lowerc[4]):
pass
elif i==5 and (n==lowerc[5]):
pass
elif i==6 and (n==lowerc[6]):
pass
elif i==7 and (n==lowerc[7]):
pass
elif i==8 and (n==lowerc[8]):
pass
elif i==9 and (n==lowerc[9]):
pass
elif i==10 and (n==lowerc[10]):
pass
elif i==11 and (n==lowerc[11]):
pass
elif i==12 and (n==lowerc[12]):
m()
elif i==13 and (n==lowerc[13]):
pass
elif i==14 and (n==lowerc[14]):
if 'm' in name:
O()
else:
global wo
if 'b' in name and not wo or len(name)<8:
begin_fill()
fillcolor('teal')
o()
end_fill()
else:
wo=False
o()
elif i==15 and (n==lowerc[15]):
pass
elif i==16 and (n==lowerc[16]):
pass
elif i==17 and (n==lowerc[17]):
r()
elif i==18 and (n==lowerc[18]):
s()
elif i==19 and (n==lowerc[19]):
t()
elif i==20 and (n==lowerc[20]):
pass
elif i==21 and (n==lowerc[21]):
pass
elif i==22 and (n==lowerc[22]):
pass
elif i==23 and (n==lowerc[23]):
pass
elif i==24 and (n==lowerc[24]):
y()
elif i==25 and (n==lowerc[25]):
pass
elif i==26 and (n==lowerc[26]):
pass
else:
print("Sorry, that letter is not yet supported!")
checkName(name)
# cdlane's answer is great and works perfect
# if only I seen this answer before i wrote the if-else monster
# I will be giving cdlane an uptick
"""
# testing
letters_to_code = {'a': a}
for letter in name:
if letter in letters_to_code:
letters_to_code[letter]()
"""
done()
我正在用 python 乌龟做点什么。下面显示的 eval 函数应该是 运行 h() 或 i()。这些是目前唯一可用的功能,因此唯一可用的是 hi 或 ih 或 i 或 h。控制台returns一个notimplementederror: eval is not yet implemented。我不明白,因为我进入了一个全新的事物,并输入了像 eval('1') 这样的基本代码,但这也不起作用。谢谢 顺便说一下,我是 python 的新手,所以我不擅长,所以如果我犯了愚蠢的错误,请注意我。
import turtle
from time import sleep
ninja = turtle.Turtle()
ninja.hideturtle()
coordinate1 = ninja.xcor()
coordinate2 = ninja.ycor()
new1=''
new2=''
ninja.speed(10)
def h():
ninja.left(90)
ninja.forward(50)
ninja.back(100)
ninja.forward(50)
ninja.right(90)
ninja.forward(35)
ninja.left(90)
ninja.forward(-50)
ninja.forward(100)
coordinate1 = ninja.xcor()
coordinate2 = ninja.ycor()
new1 = coordinate1+50
ninja.penup()
ninja.goto(new1,0)
def i():
ninja.forward(20)
ninja.pendown()
ninja.left(90)
ninja.st()
ninja.right(90)
ninja.stamp()
ninja.ht()
ninja.penup()
ninja.back(20)
ninja.pendown()
ninja.back(50)
coordinate1 = ninja.xcor()
new1 = coordinate1+50
ninja.penup()
ninja.goto(new1,0)
h()
i()
name = input('What is your name. It will be drawn in the tab to the left lowercase only please.')
print('The name will begin to draw in the tab to the left')
sleep(3)
ninja.clear()
ninja.goto(0,0)
name = list(name)
print(name)
length = len(name)
x=0
while (x < length-1):
print(name[x])
x = x + 1
new2=name[x]+'()'
print(new2)
eval(new2)
您不需要也不希望 eval()
实现此程序 -- 您可以使用将字符映射到函数的字典来实现。下面是使用字典而不是 eval()
:
import turtle
def h():
ninja.pendown()
ninja.left(90)
ninja.forward(100)
ninja.backward(50)
ninja.right(90)
ninja.forward(40)
ninja.left(90)
ninja.forward(50)
ninja.backward(100)
ninja.right(90)
ninja.penup()
ninja.forward(25)
def i():
ninja.forward(20)
ninja.left(90)
ninja.forward(70)
ninja.right(90)
ninja.pendown()
ninja.circle(5)
ninja.penup()
ninja.left(90)
ninja.backward(20)
ninja.pendown()
ninja.backward(50)
ninja.right(90)
ninja.penup()
ninja.forward(45)
letters_to_code = {"h": h, "i": i}
name = input('What is your name? Lowercase only please: ')
ninja = turtle.Turtle()
ninja.penup()
for letter in name:
if letter in letters_to_code:
letters_to_code[letter]()
ninja.hideturtle()
turtle.done()
一些需要思考的事情:
具有标准字母高度、宽度和间距距离并将它们定义为您在实现字母时可以使用的变量。
为了下一个字母,让每个字母在海龟完成时保持其原始起始方向。这将允许您以任何顺序组合字母而不会感到意外。
如果您使用的是 trinket,最好的办法是绘制出从 a 到 z 的所有字母函数,并使用 if 语句指定要打印的字母以及要打印到屏幕上的最大字母数,具体取决于如何你把每个字母都画大了。这是我第一次看到饰品,我必须承认它既令人愉快又令人沮丧。
我以为我们都被卡在了 Cache-22 中,因为 trinket。io/python 不接受 eval 或全局变量,但是 repl.it 接受使用 eval 和全局变量,但是 repl.it 没有海龟屏
“cdlane 来拯救世界”
感谢 cdlane 的回答,我知道了一种不使用 globals() 的方法。
我用 eval、exec 和 globals() 尝试了所有不同的方法,但是 Interactive Python 3 Trinket online 不接受任何这些方法。
如果输入确切的名称,代码将从函数中打印出 momo 或 AstroBoy。这些是唯一支持的字母。如果这些字母中的任何一个以不同的顺序输入,结果可能有点不可预测,但仍然令人愉快。输入“mo”、“momo”、“mom”、“astro”、“astroboy”、“boy”、“bboy”、“oboy”、“yo boy”、“robot”等进行测试
checkName(name) 函数和 if 语句的编写是为了激励任何人绘制函数字母表,现在使用已知的解决方法来解决无法使用 globals() 的问题。 (通过使用长 if-else 怪物,或使用 cdlane 的天才答案)
虽然探索 Trinket 的乐趣很有趣,但我可能不会很快再玩 trinket,或者我可能会在遥远的未来某个时候回来玩 Trinket。如果我这样做,我不会在 Trinket 上花费太多时间,因为我开始了解 Pygame,因此 Pygame 是我的下一个预定体验。
cdlane 获得赞成票!
尽情享受吧!
在 https://trinket.io/python
测试# Tested at https://trinket.io/python
# Written by AstroBoy R (featuring mOmO)
# Also featuring cdlane's workaround instead of using globals
import turtle
from time import sleep
ninja = turtle.Turtle()
ninja.hideturtle()
coordinate1 = ninja.xcor()
coordinate2 = ninja.ycor()
new1=''
new2=''
ninja.speed(10)
def h():
ninja.left(90)
ninja.forward(50)
ninja.back(100)
ninja.forward(50)
ninja.right(90)
ninja.forward(35)
ninja.left(90)
ninja.forward(-50)
ninja.forward(100)
coordinate1 = ninja.xcor()
coordinate2 = ninja.ycor()
new1 = coordinate1+50
ninja.penup()
ninja.goto(new1,0)
def i():
ninja.forward(20)
ninja.pendown()
ninja.left(90)
ninja.st()
ninja.right(90)
ninja.stamp()
ninja.ht()
ninja.penup()
ninja.back(20)
ninja.pendown()
ninja.back(50)
coordinate1 = ninja.xcor()
new1 = coordinate1+50
ninja.penup()
ninja.goto(new1,0)
h()
i()
name = input('What is your name. It will be drawn in the tab to the left lowercase only please.')
print('The name will begin to draw in the tab to the left')
sleep(3)
#ninja.clear()
ninja.goto(0,0)
print(name)
# "AstroBoy's Turtle Program (mOmO remix)"
from turtle import *
def m():
pendown()
begin_fill()
left(90)
forward(75)
right(90)
forward(30)
print(pos())
right(70)
forward(40)
print(pos())
left(140)
forward(40)
right(70)
forward(30)
right(90)
forward(75)
right(90)
forward(30)
right(90)
forward(40)
left(160)
forward(40)
right(135)
forward(40)
lt(155)
fd(40)
rt(90)
fd(29)
end_fill()
penup()
right(180)
forward(105)
print(pos())
def O():
fd(35)
pendown()
begin_fill()
circle(40)
end_fill()
pu()
left(90)
fd(30)
right(90)
pd()
begin_fill()
fillcolor('white')
circle(10)
end_fill()
pu()
right(90)
fd(30)
left(90)
fd(50)
print(pos())
def a():
pd()
lt(70)
fd(50)
rt(140)
fd(50)
pu()
lt(180)
fd(20)
lt(70)
pd()
fd(22)
pu()
lt(90)
fd(20)
lt(90)
fd(30)
def s():
fd(20)
pd()
circle(12,-90)
pu()
circle(12,90)
pd()
circle(12,90)
circle(12,90)
circle(-12,270)
pu()
fd(36)
lt(90)
fd(20)
def t():
fd(5)
pd()
lt(90)
fd(46)
lt(90)
fd(20)
rt(180)
fd(40)
rt(90)
pu()
fd(45)
lt(90)
fd(10)
def r():
pd()
lt(90)
fd(45)
rt(90)
fd(15)
if name=='robot':
begin_fill()
fillcolor('teal')
circle(-13,180)
fd(15)
rt(180)
fd(10)
pd()
if name=='robot':
end_fill()
rt(45)
fd(28)
lt(45)
pu()
fd(18)
def o():
fd(10)
pd()
circle(15,90)
fd(15)
circle(15,90)
fd(5)
circle(15,90)
fd(15)
circle(15,90)
fd(5)
pu()
fd(25)
print(pos())
def b():
fd(10)
lt(90)
pd()
begin_fill()
fillcolor('yellow')
fd(45)
rt(90)
fd(20)
circle(-11,180)
fd(10)
rt(180)
fd(10)
circle(-12,180)
fd(21)
end_fill()
pu()
lt(180)
fd(47)
def y():
fd(5)
pd()
begin_fill()
fillcolor('green')
fd(5)
pencolor('green')
lt(90)
pencolor('black')
fd(45)
lt(90)
pencolor('teal')
fd(20)
lt(135)
fd(22)
rt(45)
end_fill()
begin_fill()
fillcolor('chartreuse')
pencolor('dark green')
fd(29)
lt(90)
fd(9)
end_fill()
begin_fill()
pencolor('black')
lt(90)
fillcolor('lime')
fd(29)
rt(45)
fd(22)
lt(135)
fd(20)
end_fill()
pu()
rt(180)
fd(30)
rt(90)
fd(45)
lt(90)
def testingColorfill():
home()
pd()
c=['green','red','orange','blue', 'yellow', 'magenta', 'gold', 'pink', 'lime', 'cyan']
n=100
nc=0
while n >= 10:
begin_fill()
fillcolor(c[nc])
circle(n)
end_fill()
n = n-10
nc += 1
def testingmOmO():
m()
#Second letter color
fillcolor('yellow')
O()
#Third letter color
fillcolor('dodger blue')
m()
#Fourthletter color
fillcolor('green')
O()
testingColorfill()
testingAstroBoy()
def testingAstroBoy():
penup()
goto(-175, 0)
goto(-175, -80)
a()
s()
t()
r()
o()
b()
#color second letter o teal
begin_fill()
fillcolor('teal')
o()
end_fill()
y()
#Go to first letter position
penup()
goto(-170, 0)
goto(-170, -160)
fillcolor('crimson')
#testingmOmO()
import string
lowerc = string.ascii_lowercase
upperc = string.ascii_uppercase
momo = 'momo'
name = name.lower()
def checkmOmO(name):
if name == momo or len(name)==4:
testingmOmO()
wo=True
def checkName(name):
if len(name) > 8:
print "Sorry name is too long!"
return None
elif 'm' in name and len(name)==4:
checkmOmO(name)
return
else:
for n in name:
for i in range(len(lowerc)):
if i==0 and (n==lowerc[0]):
a()
elif i==1 and (n==lowerc[1]):
b()
elif i==2 and (n==lowerc[2]):
pass
elif i==3 and (n==lowerc[3]):
pass
elif i==4 and (n==lowerc[4]):
pass
elif i==5 and (n==lowerc[5]):
pass
elif i==6 and (n==lowerc[6]):
pass
elif i==7 and (n==lowerc[7]):
pass
elif i==8 and (n==lowerc[8]):
pass
elif i==9 and (n==lowerc[9]):
pass
elif i==10 and (n==lowerc[10]):
pass
elif i==11 and (n==lowerc[11]):
pass
elif i==12 and (n==lowerc[12]):
m()
elif i==13 and (n==lowerc[13]):
pass
elif i==14 and (n==lowerc[14]):
if 'm' in name:
O()
else:
global wo
if 'b' in name and not wo or len(name)<8:
begin_fill()
fillcolor('teal')
o()
end_fill()
else:
wo=False
o()
elif i==15 and (n==lowerc[15]):
pass
elif i==16 and (n==lowerc[16]):
pass
elif i==17 and (n==lowerc[17]):
r()
elif i==18 and (n==lowerc[18]):
s()
elif i==19 and (n==lowerc[19]):
t()
elif i==20 and (n==lowerc[20]):
pass
elif i==21 and (n==lowerc[21]):
pass
elif i==22 and (n==lowerc[22]):
pass
elif i==23 and (n==lowerc[23]):
pass
elif i==24 and (n==lowerc[24]):
y()
elif i==25 and (n==lowerc[25]):
pass
elif i==26 and (n==lowerc[26]):
pass
else:
print("Sorry, that letter is not yet supported!")
checkName(name)
# cdlane's answer is great and works perfect
# if only I seen this answer before i wrote the if-else monster
# I will be giving cdlane an uptick
"""
# testing
letters_to_code = {'a': a}
for letter in name:
if letter in letters_to_code:
letters_to_code[letter]()
"""
done()