间距列表的语法无效

时间:2012-12-03 04:16:31

标签: 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 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

1 个答案:

答案 0 :(得分:1)


首先,你错过了一个“:”

def stop_max_changes():  # missing : there

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

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)

请完整的代码,我可以在这里进行全面诊断..