Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with casting int to float on Python 3 with get_random_food_position() #236

Open
m0nt4ld0 opened this issue Aug 18, 2024 · 1 comment

Comments

@m0nt4ld0
Copy link

Problem Description:

I noticed that in the get_random_food_position() function, the code uses the random.randint() function with arguments that might be float values due to division. This is causing a TypeError because random.randint() expects integer values, not floats.

Currently, the code looks like this:
x = random.randint(-w / 2 + food_size, w / 2 - food_size)

And I'm getting this error trying to run this on Python 3
image

Why is it important?

This issue can lead to crashes or unexpected behavior during runtime.

Possible Solution:

🎯 My proposal is to ensure that the arguments passed to random.randint() are cast to integers. We can do this by either using int() to cast the values or by using integer division //.

Alternative 1:

def get_random_food_position():
    x = random.randint(int(-w / 2 + food_size), int(w / 2 - food_size))
    y = random.randint(int(-h / 2 + food_size), int(h / 2 - food_size))
    return (x, y)

Alternative 2:

def get_random_food_position():
    x = random.randint(-w // 2 + food_size, w // 2 - food_size)
    y = random.randint(-h // 2 + food_size, h // 2 - food_size)
    return (x, y)
@m0nt4ld0
Copy link
Author

m0nt4ld0 commented Aug 18, 2024

@Shahrayar123 I've been fixing this on PR #237 please review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant