Why Python is not working for me at all?


#1



I'm stuck at creating scripts and using them outside this website.
I downloaded python and when i try for example to use my exercise of Battleship, just not working at all.

I get errors like invalid syntax, print is not working, etc...

I just want to get running scripts and test outside the site.
Hopefully i can get help and continue learning from this amazing website.


from random import randint

board = []

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

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

print "Let's play Battleship!"
print_board(board)

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

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

ship_row = random_row(board)
ship_col = random_col(board)
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4): 
    print "Turn", turn + 1
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))

    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
    print (turn + 1)
if turn == 3:
    print "Game Over"
    print_board(board)


#2

codecademy runs python2 (2.7.3 i believe), you most likely run python3 (3.4 or 3.5)

there are a few differences, one of the key ones is print, python2:

print "hello world"

python3:

print("hello world")

other important difference for your code, raw_input no longer exist, it is now input


#3

Hello stetim94, thanks for reply so fast.

Could u help me with that? Why im geting fail on that code part?

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

#4

like i tried to explain (you would have to validate, i can't do that for you), you run python3, and python3 requires parentheses (brackets) for print statements:

print(" ".join(row))

codecademy runs python2, then brackets are not required. I am sure that if you search online, you might find python2 to python3 converters


#5

Ok i get it now, thanks a lot will try to fix that code and play around with it.

Last question is it possible to run scripts like the battleship one in android? It wont work just with opening it right?


#6

don't forget to replace raw_input with input (raw_input no longer exist in python3)

it seems possible (resource), but android is natively Java, not python. If you plan to build something for android, i wouldn't choice python as language


#7

Thanks a lot, its working fine now! Just a few changes.

Amazing how fast u answered me.

Have to go! Have a nice day stetim94