Python,通过一个函数突破循环

时间:2017-08-05 20:58:58

标签: python function loops break

我有一个问题。我试图打破一个函数之外的循环。 我使用了一个休息,但它没有用,之后我试图用一个标志打破for循环,当玩家获胜时我将它从“False变为True”。 请检查评论,,, 我非常感谢您提供的任何帮助。

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

0 个答案:

没有答案