未在python中用eval函数实现错误

时间:2019-03-10 06:25:37

标签: python turtle-graphics

我正在用python turtle做东西。如下所示的eval函数应该运行h()或i()。这些是目前唯一起作用的功能,因此起作用的唯一事物是hi或ih或i或h。控制台返回一个notimplementederror:尚未实现eval。我不明白,因为我进入了一个全新的事物,并放入了诸如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)

1 个答案:

答案 0 :(得分:0)

如果您使用小饰品,最好的办法是绘制从 a 到 z 的所有字母函数,并使用 if 语句指定要打印的字母以及要打印到屏幕上的最大字母数,具体取决于您的大小画出每个字母。这是我第一次看到小饰品,我必须承认它有点令人愉快,同时也有点令人沮丧。

我以为我们都被困在 Cache-22 中,因为 trinket.io/python 不接受 eval 或 globals,但是 repl.it 接受使用 eval 和 globals,但 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()