Python-Turtle:将字符串列表转换为工作代码

时间:2013-08-22 01:15:41

标签: python string list turtle-graphics

#There are large test lists of strings which need to be turned
# into workable code. But i keep getting 'cant assign to literal' error
from turtle import *

#Define the scripting language
'lift pen' = pu()

'lower pen' = pd()

'thin lines' = pensize(1)

'thick lines' = pensize(5)

'black pen' = color('black')

'coloured pen' = color('green')

'go to',x,y = goto(x,y)

'draw dot', s = dot(s)



def draw(test):

    home()
    total = len(test)
    list_num = 0
    while total > 0:
        print test[list_num] 
        list_num = list_num + 1 
        total = total - 1
    hideturtle()

draw(puzzle)

2 个答案:

答案 0 :(得分:2)

"hello"之类的东西在python中被称为字符串“literals”。它们代表不可变的字符序列。您不能将值分配给字符串文字。

如果您尝试以可以通过字符串表示形式引用和调用它们的方式存储命令,请尝试以下操作:

commands = {}
commands['lift pen'] = pu
commands['lower pen'] = pd
commands['thin lines'] = lambda:pensize(1)
commands['thick lines'] = lambda:pensize(5)
commands['black pen'] = lambda:color('black')
commands['coloured pen'] = lambda:color('green')
commands['go to'] = lambda x, y:goto(x,y)
commands['draw dot'] = lambda s:dot(s)

然后您可以使用以下内容访问命令:

commandList = [("lift pen", []), ("go to", [10, 20]), ("lower pen", [])]

for command, args in commandList:  # Loop over each command and associated arguments
    commands[command](*args)       # Call the function stored under the key given by the command with the args.

您需要首先将“大型字符串测试列表”解析为上述格式,但如果没有看到它开始的格式,我无法帮助您。

答案 1 :(得分:0)

#example of the list being used

conundrum = ['black pen',
'lift pen',
['go to', -98, 132],
['draw dot', 10],
'lift pen',
['go to', -120, 137],
['draw dot', 10],
'lift pen',
'thick lines',
['go to', -55, 80],
'lower pen']

#Import everything from the turtle library
from turtle import *

#Define the draw function

def draw(test):
    home()
    #'total' is used as the amount of loops left
    # and amount of strings left to test
    #'current_num' is set as the current string
    # being tested starting from the first in
    # the list.
    total = len(test)
    current_num = 0
    bgcolor('black')

    # A while loop is set to run until total = 0
    #which means all strings have been checked

    while total > 0:
        #A set of if/elif statements to determine
        #what each string is and what statement
        #to use given that string

        if test[current_num] == 'lift pen':
            pu()

        elif test[current_num] == 'lower pen':
            pd()

        elif test[current_num] == 'thin lines':
            pensize(1)

        elif test[current_num] == 'thick lines':
            pensize(5)

        elif test[current_num] == 'black pen':
            color('black')

        elif test[current_num] == 'coloured pen':
            color('blue')

        #This last elif statement, tests to see if
        # it is a list rather than a string

        elif type(test[current_num]) == list:

            #As there are only 2 types of lists
            #one with 3 variables and the other with two
            #a test to determine which has 3 is run.

            if len(test[current_num]) == 3:

                # if this is true then the list is the goto() list
                # so the second variable in the list is set as x
                # and the third variable as y in goto(x,y)

                goto((test[current_num][1]),(test[current_num][2]))

            #else it is the dot() list

            else:

                 dot(test[current_num][1])

        #A final backup else statement that will print any strings that
        #dont follow with the set if statements so it can be fixed

        else:
            print test[current_num]

        #at the end of each loop the current_num is raised so the next
        #string will be tested while the total is reduced so that the
        #program can stop when it reaches 0

        current_num = current_num + 1 
        total = total - 1
    hideturtle()
    done()

然后我所做的就是调用绘图功能并且它可以正常工作