未在Python中使用Eval函数实现错误



我正在用Python Turtle做点什么。下面显示的评估函数应该运行H()或I()。这些是目前唯一起作用的功能,因此唯一有效的是HI或IH或I或H。控制台返回NotimplementedError:尚未实施评估。我不明白,因为我进入了一件全新的事物,并放置了诸如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()

有些要考虑的事情:

  • 有一个标准字母 height width 间距距离距离将它们定义为变量,在暗示字母时可以使用。

  • 为下一个字母的利益,每个字母都会在其原始起始方向上留下乌龟的原始起始定向。这将使您能够以任何顺序组合字母而不会感到惊讶。

如果您使用的小饰品,最好的选择是从a到z绘制所有字母函数,并使用if语句指定要打印的字母以及要打印的最大字母数量在屏幕上取决于您绘制每个字母的大小。这是我第一次见到小饰品,我必须承认这有点愉快,有些令人沮丧。

我以为我们都陷入了Cache-22屏幕。

" cdlane在这里保存一天

感谢CDLane的回答,我知道不使用Globals()的方法。

我通过评估,Exec和Globals()尝试了所有不同的方式,但是Interactive Python 3 Frinket Online不接受任何这些方法。

如果输入该确切名称,则代码将从功能中打印出MOMO或Astroboy。这些是唯一支持的字母。如果这些字母中的任何一个以不同的顺序键入,则结果可能会有些令人振奋,但仍然令人愉快。通过打字来测试" mo"妈妈,"妈妈"; robot&quot。等等。

checkName(名称)函数以及是否写了语句来激发任何人绘制功能字母,现在使用已知的工作来无法使用Globals()。(通过使用长if-else怪物,或使用CDLane的天才答案

尽管探索饰品的乐趣很有趣,但我可能很快就不会再和小饰品一起玩,或者也许我会在遥远的将来卷土重来。如果我这样做的话,我不会花很多时间在小饰品上,因为我开始了解pygame,因此,pygame是我的下一个预定体验。

cdlane正在获得一个upvote!

享受!

在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()

最新更新