Python 无效语法与我的列表间隔

Python 无效语法与我的列表间隔,python,syntax,Python,Syntax,def spread_row()的语法无效 我有一个文本文件,它可以很好地工作,但当我把它复制到最终的编码中时,它就不起作用了。帮忙 我想做的是用随机的1和0做一个3x3的网格。代码是不完整的,所以一些编码还没有完成 ##variables to control all the games ##number of games played and points collected in total games_so_far = 0 total_points = 0 ## FOR ONE GAM

def spread_row()的语法无效

我有一个文本文件,它可以很好地工作,但当我把它复制到最终的编码中时,它就不起作用了。帮忙

我想做的是用随机的1和0做一个3x3的网格。代码是不完整的,所以一些编码还没有完成

##variables to control all the games
##number of games played and points collected in total
games_so_far = 0
total_points = 0

## FOR ONE GAME
## variables that will be used for one single game
max_change = 0

## SUBROUTINES AND FUNCTIONS

import random

def invite_to_play():
  play = raw_input("Would you like to play " + ask_again + "? (y/n): ")
  if play == "y" or play == "Y":
    play_response = True
  else:
    play_response = False
  return play_response

def computer_play():
  computer = raw_input("Would you like that the computer also plays? (y/n): ")
  if computer =="y" or computer =="Y":
    computer_response = True
  else:
    computer_response = False
  return computer_response

def ask_user_int(question):
  response = raw_input(question)
  while not (response.isdigit()):
    print "Your input must be an integer number"
    response = raw_input(question)
  return int(response)

def generate_random_number(dim):
  ## Generate a board of 0's and 1's, up to the size set in "dim"
  return [random.randint(0,1) for i in range(dim)

def spread_row():
  ## Spaces the numbers evenly
  for dat in data:
        print "     ".join(map(str, dat))
  return data

def stop_max_changes()
  print "No more changes, the game is over!"
  return

def program_evaluates_board(numbers):
  ## Evaluates the rows and cols to see if they are even or odd
  row1 = sum(data[0])
  col1 = sum(row[0] for row in data)
  row2 = sum(data[1])
  col2 = sum(row[1] for row in data)
  row3 = sum(data[2])
  col3 = sum(row[2] for row in data)
  if row1%2 == 0:
    print "False"
  else:
    print "True"
  return 


## TOP LEVEL


print 'Welcome to the "An odd matrix" game' + \
      "====================================="

games_so_far = 0

wants_to play = invite_to_play("")
computer = computer_play()



## LOOP TO PLAY MORE GAMES
while wants_to_play:
  games_so_far = games_so_far + 1

  num = ask_user_int("Size of board (between 3 and 6 inclusive): ")
  dim = int(num)

  data = [random_row(dim) for i in range(dim)]

  spread_row()

  print "The board is"
  print "-------------"

  print "\n(initial board)"

  print "\n         Col 0   Col 1   Col 2"

  max_changes = ask_user_int("How many changes would you like to do?" + \
                       "\n > 0 and <= 2: ")
  changes_so_far = 0
控制所有游戏的变量 ##进行的游戏数量和收集的总积分 游戏迄今为止=0 总分=0 ##一场比赛 ##将用于单个游戏的变量 最大变化=0 ##子程序和函数 随机输入 def invite_to_play(): 播放=原始输入(“您想播放”+再次询问+“?(y/n):”) 如果播放==“y”或播放==“y”: play_response=True 其他: play_response=False 返回播放响应 def computer_play(): 计算机=原始输入(“您希望计算机也播放吗?(y/n):”) 如果计算机==“y”或计算机==“y”: 计算机响应=真 其他: 计算机响应=错误 返回计算机\u响应 def ask_user_int(问题): 响应=原始输入(问题) 而不是(response.isdigit()): 打印“您的输入必须是整数” 响应=原始输入(问题) 返回int(响应) def生成随机数(dim): ##生成由0和1组成的电路板,最大尺寸为“dim”中设置的尺寸 返回[random.randint(0,1)表示范围内的i(dim) def spread_row(): ##将数字平均隔开 对于数据中的dat: 打印“.join(映射(str,dat)) 返回数据 def stop_max_更改() 打印“不再更改,游戏结束!” 返回 def程序板(编号): ##计算行和列以查看它们是偶数还是奇数 行1=总和(数据[0]) col1=总和(数据中的行为第[0]行) 第2行=总和(数据[1]) col2=总和(数据中的行为第[1]行) 第3行=总和(数据[2]) col3=总和(第[2]行表示数据中的行) 如果第1行%2==0: 打印“假” 其他: 打印“真” 返回 ##顶级 打印“欢迎来到“奇数矩阵”游戏”+\ "=====================================" 游戏迄今为止=0 想要玩=邀请玩(“”) 电脑=电脑游戏 ##循环以玩更多游戏 当你想玩的时候: games\u so\u far=games\u so\u far+1 num=ask_user_int(“板的大小(包括3到6之间):”) dim=int(num) 数据=[范围(dim)内i的随机_行(dim)] 铺开 打印“电路板是” 打印“------------” 打印“\n(初始板)” 打印“\n第0列第1列第2列” max_changes=询问_user_int(“您希望做多少更改?”+\ “\n>0和
首先,您缺少一个“:”

第二,为什么这个返回值中有空格

wants_to play = invite_to_play("")  # why space here ?
第三,你缺少一个正确的括号

def generate_random_number(dim):
  ## Generate a board of 0's and 1's, up to the size set in "dim"
  return [random.randint(0,1) for i in range(dim)

请通过完整的代码,我可以在这里进行完整的诊断。

您是否使用空格和制表符?(这不是一件好事。)您应该能够通过
python-t file.py
.I”来判断我使用空格。我应该放什么呢?这可能不是你的最终错误,但是你说的是数据中的dat
,而没有在函数中声明数据。看起来你应该在那里传递数据,然后返回一些东西给你的主函数,比如
数据=行(数据)
。只要您与每个块的空格使用情况保持一致,就可以了。另外,还建议您使用4个空格,而不是制表符。这是完整的代码,我还没有完成其余部分的编写。我正在修复所有每个人都会发现的错误,但即使如此,def spread_row()中仍然存在无效语法@user1870196:不,在
返回[random.randint(0,1)for i in range(dim)
之前的
def spread_row()中缺少
]
本身就可以。@user1870196:这不是一个猜谜游戏。准确地发布您遇到问题的代码和回溯。修复丢失的
后,
中的额外空间希望您玩
并添加额外的“]`,您的代码没有语法错误。
def generate_random_number(dim):
  ## Generate a board of 0's and 1's, up to the size set in "dim"
  return [random.randint(0,1) for i in range(dim)