0

Python で単純な戦艦ゲームのコーディングを終えたところです。ターミナルでは問題なく動作しますが、cgi-bin スクリプトとしてオンラインで実行しようとすると問題が発生します。私のコードは次のとおりです。

#!/usr/bin/python

print "Content-type: text/html\n\n"
print "<html>"

from random import randint

board = []

for x in range(0,5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)

print_board(board)

def random_row(board):
  return random.randint(0,len(board)-1)

def random_col(board):
  return random.randint(0,len(board[0])-1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = raw_input("Guess Row:")
guess_col = raw_input("Guess Col:")

print ship_row
print ship_col

if (guess_row == ship_row and guess_col == ship_col):
print "Congratulations! You sank my battleship!<br/>"
else:
if board[guess_row][guess_col] == "X":
print "You guessed that one already.<br/>"
if not (0 <= guess_row < len(board)) or not (0 <= guess_col < len(board)):
print "Oops, that’s not even in the ocean.<br/>"
else:
print "You missed my battleship!<br/>"
board[guess_row][guess_col] == "X"
print_board(board)

print "</html>"

他の単純な hello-world スクリプトは Web ページで問題なく実行されることに注意してください。エラーログは、次に従ってこれらの行を返します。

[Sun Jan 06 20:34:26 2013] [error] [client xxx]   File "
[Sun Jan 06 20:34:26 2013] [error] [client xxx] /usr/lib/cgi-bin/game.py
[Sun Jan 06 20:34:26 2013] [error] [client xxx] ", line
[Sun Jan 06 20:34:26 2013] [error] [client xxx] 34
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx] print "Congratulations! You sank my battleship!<br/>"
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx] ^
[Sun Jan 06 20:34:26 2013] [error] [client xxx] IndentationError
[Sun Jan 06 20:34:26 2013] [error] [client xxx] :
[Sun Jan 06 20:34:26 2013] [error] [client xxx] expected an indented block
[Sun Jan 06 20:34:26 2013] [error] [client xxx]
[Sun Jan 06 20:34:26 2013] [error] [client xxx] Premature end of script headers: game.py

ここでこれを手伝ってくれませんか?

前もって感謝します!

ps:私はpythonが初めてです:D

4

2 に答える 2

0

これを試して:

#!/usr/bin/python

print "Content-type: text/html\n\n"
print "<html>"

from random import randint

board = []

for x in range(0,5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print_board(board)

def random_row(board):
    return random.randint(0,len(board)-1)

def random_col(board):
    return random.randint(0,len(board[0])-1)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = raw_input("Guess Row:")
guess_col = raw_input("Guess Col:")

print ship_row
print ship_col

if (guess_row == ship_row and guess_col == ship_col):
    print "Congratulations! You sank my battleship!<br/>"
elif board[guess_row][guess_col] == "X":
    print "You guessed that one already.<br/>"
if not (0 <= guess_row < len(board)) or not (0 <= guess_col < len(board)):
    print "Oops, that’s not even in the ocean.<br/>"
else:
    print "You missed my battleship!<br/>"
board[guess_row][guess_col] == "X"
print_board(board)

print "</html>"
于 2013-01-06T18:44:41.793 に答える