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 脚本在网页中运行良好。错误日志根据以下内容返回这些行:

[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 回答
0

帖子有点旧,但万一其他人正在寻找:

尝试运行它(从您的浏览器),看看它是否有效:

#!/usr/bin/python
import cgi
cgi.test()

如果这没有给出错误,请尝试删除文件,创建一个新文件并将代码复制/粘贴回去。

我刚刚解决了我的问题。

于 2013-08-03T10:05:28.920 回答