Where do I correctly put this break statement in Python? -


i need put break statement, instructions of code say: break should go inside if statement, right after "congratulations!" message.

from random import randint  board = []  x in range(5):     board.append(["o"] * 5)  def print_board(board):     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) print ship_row print ship_col  guess_row = int(raw_input("guess row:")) guess_col = int(raw_input("guess col:"))  if guess_row == ship_row , guess_col == ship_col:         break print "congratulations! sunk battleship!" #i want put break satement here, how?     else:     if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):         print "oops, that's not in ocean."      elif(board[guess_row][guess_col] == "x"):         print "you guessed 1 already."     else:         print "you missed battleship!"         board[guess_row][guess_col] = "x"     # print (turn + 1) here!     print_board(board)     if turn ==0:         print "game over" 

you need while loop , set variable turn value, decreasing 1 if battleship missed.

turn = 5 #set turn  while true:     guess_row = int(raw_input("guess row:"))     guess_col = int(raw_input("guess col:"))      if guess_row == ship_row , guess_col == ship_col:         print "congratulations! sunk battleship!" # print before break         break      #i want put break statement here, how?     else:         if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):             print "oops, that's not in ocean."          elif(board[guess_row][guess_col] == "x"):             print "you guessed 1 already."         else:             print "you missed battleship!"             turn-=1  # decrease turn if player misses             board[guess_row][guess_col] = "x"     # print (turn + 1) here!         print_board(board)         if turn ==0:             print "game over"             break 

bear in mind also, if user enters non int value , program crash.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -