首页 > 解决方案 > 如何设置具有多种功能的for循环?

问题描述

这个错误与我如何设置循环有关。其余代码对这个问题没有影响。 这是错误消息。

根据提供的数据集在网格上绘制竞争对手

def random_moves(the_seed = None, max_rounds = 35):
    print('Here are the randomly-generated moves:')
    seed(the_seed)
    competitors = ['Competitor A', 'Competitor B', 'Competitor C', 'Competitor D',]
    shuffle(competitors)
    num_rounds = randint(0, max_rounds)
    moves = []
    for round_no in range(num_rounds):
        print()
        for competitor in competitors:
            move = [competitor, choice(['Left', 'Right', 'Up', 'Down'])]
            print(move)
            moves.append(move)
    print('\nThere were', len(competitors) * num_rounds,
          'moves generated in', num_rounds,
          ('round' if num_rounds == 1 else 'rounds'))
    return moves

def process_moves(random_moves):
    vertical = 90
    horizontal = 120
    position = [[-3*horizontal,3*vertical,'A',7],[3*horizontal,3*vertical,'G',7],[-3*horizontal,-3*vertical,'A',1],[3*horizontal,-3*vertical,'G',1]]
    penup()
    
    occupied = [[('A',7)],[('G',7)],[('A',1)],[('G',1)]] #To store the grid occupied
      
    pencolor('red')
    setpos(position[0][0],position[0][1])
    write('A',align='left',font=20)
    
    pencolor('blue')
    setpos(position[1][0],position[1][1])
    write('B',align='center',font=20)
    
    pencolor('green')
    setpos(position[2][0],position[2][1])
    write('C',align='center',font=20)
    
    pencolor('yellow')
    setpos(position[3][0],position[3][1])
    write('D',align='center',font=20)
      
for move in random_moves:
    competitor, direction = move[0],move[1]
    showturtle()
    if competitor == 'Competitor A':
        pencolor('red')
        if direction == 'Left':
            if position[0][2]=='A':
                continue
            else:
                position[0][0] = position[0][0] - horizontal
                position[0][2]=chr(ord(position[0][2])-1)
        elif direction == 'Right':
            if position[0][2]=='G':
                continue

标签: pythonpython-3.xturtle-graphics

解决方案


您在调用 . 时缺少括号random_moves

改变这个:

for move in random_moves:

对此:

for move in random_moves():

您可能还需要指定参数。


推荐阅读