I have an idea of how to add stats, but I don’t know how to include it in the code.
‘’‘For stats, you have to .append to a list, probably with an if conditional. You have to store the value of when game_win is true (or knowing when the game is over), and then appending to the list with the argument that signifies a win.’’’
Yeah, so I want to be able to do that for the extra part of the ‘battleship’ project, but I don’t know how to track the win score, so that I can append it as an argument to the stats list. The win score is just going to be the turn when the game was won.
Rematches are a different story, but one step at a time.
these stats will only work for as long as the program is running (so rematches). If the user closes the program, lists are cleared. Do you want to persist the data? Then you will need to write the data to the hard drive (file or databases are options) and read these at program boot
yes and no. The only programming language supported by browsers is Javascript, so that would require transpiling your python code into JS code, which i think is technically possible with certain Python libraries, but it adds a lot of complexity.
Then i would just write the program in JS.
But i am slightly confused as to why you picked python if writing to browser cache was your intention all along? Pick the right tool (language) for the job.
Nah, just curious. Anyway, here’s my code right now.
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 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)
#print ship_row
#print ship_col
turn_stat = []
# Everything from here on should be in your for loop
# don't forget to properly indent!
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 sank the metal ■■■■!"
game_win = True
turn_stat.append(turn)
print "Last games were finished in the following number of turns:" \ turn_stat +1
# rematch = raw_input ("Rematch? ")
# if rematch = "y" or "Y":
break
else:
if guess_row not in range(5) or \
guess_col not in range(5):
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"
if turn == 3:
print "Game Over"
print_board(board)
I’m getting an ‘unexpected character after line continuation’ error on line 38. I dunno why it’s considered unexpected.
if guess_row not in range(5) or \
guess_col not in range(5):
you do it right, you use the line continuation character (\) and put the remaining code on the next line. on line 38, you just have the code on a single line, so you don’t need \
Okay, I do the line continuation correct, but then I get ‘invalid syntax’ for line 39 ‘turn_stat +1’. Doesn’t work when I make turn_stat into an integer either.
EDIT: Took out the continuation , but now it says, I can’t concatenate a list, but I don’t know why. I thought I could before. If I remove the +, then it gives syntax error
it is the lesson? To persist the data after the program is done running?
Python include sqlite3 api by default, so you could use that. As for a file, you would first need to determine what kind of file (txt, csv or something else) you want.
i wouldn’t use codecademy editor for this, its not nice given all the validation behind an exercise. Using something like repl.it would already be slightly better, but ideally create your local development environment.