2013-01-06 94 views
0

我剛剛在Python中編寫了一個簡單的戰艦遊戲。在終端中,它運行完美,但是當我嘗試在線運行它作爲cgi-bin腳本時,存在問題。 我的代碼是:Python腳本中腳本標題的提前結束

#!/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世界腳本的網頁好嗎運行。 錯誤日誌根據這個返回的線路:

[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:我在蟒蛇是新:d

+0

您發佈的代碼在終端中運行得並不順利 - 您確定修復縮進後無法正常工作嗎? –

+0

是的,我敢肯定,現在它不能在終端工作:( –

+1

你需要比「它不工作」更明確...... –

回答

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>" 
+0

同樣的事情.. –

0

帖子是有點老了,但如果別人期待:

嘗試運行這個(從你的瀏覽器),看看它是否工作:

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

如果這不會給出錯誤,然後嘗試刪除文件,金鑰匙g一個新的和複製/粘貼代碼回來。

我剛剛解決了我的問題做到這一點。

相關問題