0

I've a question. I'm trying to break a loop outside of a function . I used a break, but it didn't work , after that I'm trying to break the for loop using a flag that I'm changing it from "False to True " when the player win.. please check the comments,,, I really appreciate any help you can provide.

flag=False
def guessing(guess_row,ship_row,i,flag):

  if guess_row == ship_row and guess_col == ship_col:
    print ("Congratulations! You sunk my battleship!")
    flag=True
    print (flag,"flag must be true here inside function")
    return flag

    # Same on me I used a break here , but it didn't work
    # instead of a break here I am using a flag, but again it didn't work
  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(guess_row == False or guess_col== False):
      print ("Oops, type something !")
    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 i==3:
      print ("Game Over")
    # Print (turn + 1) here!
    print_board(board)


#print ("here" )
times=4
i = 0
for i in range(times):
  if flag==False:
    print ("  \n")
    guess_row = int(input("Guess Row: "))
    guess_col = int(input("Guess Col: "))
    print ("Turn", i + 1)
    print (flag," flag here must be False , ")
    guessing(guess_row,ship_row,i,flag)
  else:
    print ("break statement",flag)
    break
Charith Prabhagya
  • 189
  • 1
  • 3
  • 11
  • 2
    you need to add the statement `global flag` at the first statement of your function `guessing` and remove it from the parameter. – marsouf Aug 05 '17 at 21:02
  • `guessing(guess_row,ship_row,i,flag)` shouldn't it be `guessing(guess_row,guess_column,i,flag)` – Rajan Chauhan Aug 05 '17 at 21:07
  • i tried it now but is says "error :name is parameter and global" , after that i tried "global flag" outside and above of my function but it doesn't work... thx – stefanos gian Aug 05 '17 at 21:10

0 Answers0