python - 如何设置具有多种功能的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
解决方案
您在调用 . 时缺少括号random_moves
。
改变这个:
for move in random_moves:
对此:
for move in random_moves():
您可能还需要指定参数。
推荐阅读
- kotlin - 具有任何接收器类型的 Kotlin 函数文字
- javascript - 如何通过不出现在输入框中使我的字符计数器工作?
- javascript - 用数组名克隆输入字段只得到php中的第一个值?
- macros - Scheme R5RS 示例中的语法规则卫生宏调用中缺少参数
- javascript - 愚蠢的 TypeScript-ESLint 问题:返回一个未解决的 Promise
- ios - 如何将元数据谷歌语音传递给文本 api - swift ios
- java - 为什么 Gerrit 无法加载 init 进程创建的 secure.config 文件?
- python - miniconda 安装会影响标准 python 安装吗?
- rust - 当外部生命周期不同时,为什么我不能将一个引用的引用的引用分配给另一个引用?
- python - 如何切换到旧的 pyspark 版本?