如何设置具有多个功能的循环?

问题描述

错误与我设置循环的方式有关。其余代码对这个问题没有影响。 Here is the error message.

根据提供的数据集在网格上吸引竞争对手

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,'G',[-3*horizontal,-3*vertical,1],1]]
    penup()
    
    occupied = [[('A',7)],[('G',[('A',1)],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',font=20)
    
    pencolor('yellow')
    setpos(position[3][0],position[3][1])
    write('D',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

解决方法

致电random_moves时缺少括号。

更改此:

for move in random_moves:

对此:

for move in random_moves():

您可能还需要指定参数。