hellwraiz
hellwraiz

Reputation: 501

How to check what value a function will return

print('Hello world!'). I have problem with a game I am making. The problem is that I have a function that returns a value if a certain thing happened, but I have no idea how to check whether it is going to return a value I need or None, so here is a tiny bit of my program:

def checking_for_events():
    for event in pygame.event.get():
        if event.type == VIDEORESIZE:
            #some irrelevant code where I get the multiplier
            background_size = background_size * multiplier
            player_size = player_size * multipler
            return background_size, player_size

#a couple lines later

while True:
    background_size, player_size = checking_for_events()

So the first way around its all fine, but when the loop repeats and the function gets called again, it returns None, as the change needed to trigger if event.type == VIDEORESIZE: doesn't occur, so background/player_size gets changed to None, which makes the program break. So I wonder if there is a way to check whether checking_for_events() is going to return None or the 2 integers that I need, and a way to make sure that background/player_size won't get changed to None.

Upvotes: 0

Views: 58

Answers (1)

Barmar
Barmar

Reputation: 780949

You can't tell what something is going to do, only what it actually has done.

Assign the result to another variable. Check if the value is None before assigning to background_size and player_size.

while True:
    result = checking_for_events()
    if result is not None
        background_size, player_size = result

Upvotes: 4

Related Questions