Jonny
Jonny

Reputation: 1

check variable in a different def function (python)

I am trying to check in def_2_is_apple if the variable APPLE is on the game board in def_1_print_game_board but if i try it like that i only get that i need to define row and column in def_2

def_2_is_apple is a bool and should be set to True if the variable APPLE is in def_1_print_game_board


import random

BOARD_WIDTH = 8
BOARD_HEIGHT = 8
SNAKE = \[""\]
ORIENTATION = 4
APPLE = "A0"
APPLE_LIVES = 12
APPLE_GOT_EATEN = False
LIVES = 3
SCORE = 0
BIGGER_SNAKE = False


def _2_is_apple(row, column):
    global BOARD_HEIGHT
    global BOARD_WIDTH

    for row in range(BOARD_HEIGHT):
        for column in range(BOARD_WIDTH):
            if APPLE == f"{chr(row + 65)}{column}":
                return True
            else:
                return False
pass



def _1_print_game_board():
    global Board_HEIGHT
    global Board_WIDTH
     # print the game field

    print(f"Lives: {LIVES} - Apple Lives: {APPLE_LIVES} - Score: {SCORE}")
    print("----------------------------")
    for i in range(BOARD_HEIGHT):
        print(f"{chr(i + 65)} |", end="")
        for j in range(BOARD_WIDTH):
            if APPLE == f"{chr(i + 65)}{j}":
                print(" O ", end="")
            elif _3_is_snake == 1:
                print(" + ", end="")
            elif _3_is_snake == 2:
                print(" ∧ ", end="")
            elif _3_is_snake == 3:
                print(" < ", end="")
            elif _3_is_snake == 4:
                print(" v ", end="")
            elif _3_is_snake == 5:
                print(" > ", end="")
            else:
                print("   ", end="")
        print("|", end="")
        print()
    print("----------------------------")
    print("    0  1  2  3  4  5  6  7")
    pass


def main():
      #call the functions in the right order
    _1_print_game_board()
    _2_is_apple()
    _3_is_snake
    _4_move_snake()
    _5_detect_collision()
    _6_spawn_apple()
pass


if __name__ == '__main__':
    main()

tried deleting the row and column from def_2 and letting it empty but the functions seems not to intergate with def_1

Upvotes: 0

Views: 73

Answers (1)

Swifty
Swifty

Reputation: 3409

To sum up what I said in my comments, here is a correct code for the _2_is_apple function (independently of the rest of the code):

def _2_is_apple():
    for row in range(BOARD_HEIGHT):
        for column in range(BOARD_WIDTH):
            if APPLE == f"{chr(row + 65)}{column}":
                return True
    return False
            
APPLE = "A0"            
print(_2_is_apple())
# True
APPLE = "G5"
print(_2_is_apple())
# True
APPLE = "B9"
print(_2_is_apple())
# False
APPLE = "Z1"
print(_2_is_apple())
# False

Upvotes: 1

Related Questions