0
votes

I just finished coding in Python a simple battleship game. In terminal,it runs perfectly,but when i try to run it online as a cgi-bin script,there are problems. My code is:

#!/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>"

I should mention that other simple hello-world scripts run alright in the webpage. the error log returns those lines according to this:

[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

Can you help me with this over here?

Thanks in advance!

ps: i'm new in python :D

2
The code you post wouldn't run perfectly well in a terminal - are you sure after you fix the indentation it doesn't work? - Jon Clements♦
yes i'm sure and now it does not work in the terminal either :( - Kostas Livieratos
You need to be more explicit than "it does not work"... - Jon Clements♦
When i run the code without the print "<html>" and print "</html>" tags,it runs. With those tabs,it does not run in the terminal either. Also,it returns a 500 Internal error when i try to run it online. - Kostas Livieratos

2 Answers

0
votes

Try this:

#!/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
votes

Post is a bit old, but in case someone else is looking:

Try running this (from your browser) to see if it works:

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

If that doesn't give an error then try deleting the file, making a new one and copy/pasting the code back in.

I just solved my problem doing that.