我的 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