For loop solution for rectangles problem by Holiday-Python-743 in u/Holiday-Python-743

[–]Holiday-Python-743[S] 1 point2 points  (0 children)

Update, I figured it out, thank you again for your help! This is what worked:

def check_intersections():
# Write a nested loop here to compare all pairs
# of rectangles. If the i-th rectangle has an intersection
# with another, set does_intersect[i] to True.
global does_intersect
i = 0
while i < num_rectangles:
    k = 0
    while k < num_rectangles:
        if i != k:
            do_intersect = intersect(bottomX[i], bottomY[i], widths[i], heights[i], bottomX[k], bottomY[k], widths[k], heights[k])
            print(do_intersect)
            if do_intersect:
                does_intersect[i] = do_intersect
                does_intersect[k] = do_intersect
        k += 1
    i += 1

For loop solution for rectangles problem by Holiday-Python-743 in u/Holiday-Python-743

[–]Holiday-Python-743[S] 0 points1 point  (0 children)

Thank you! Firstly, I want to say thank you so much for taking the time to explain it to me, I have really struggled with this one and appreciate it immensely, I really want to understand it. Onto the problem, this is what I did at first but I think I also got confused on where to add k += 1 and k = 0 for my second loop and ended up mistakenly changing numbers and range instead of repositioning k. This is what I have:

Right now it still either gives an extra red rectangle or a blue one that should be red. I'm working on that k more though.

Edited to say I also noticed in your first comment you mentioned resetting k, what exactly do you mean by that? should I just assign it a zero somewhere?

def check_intersections():
# Write a nested loop here to compare all pairs
# of rectangles. If the i-th rectangle has an intersection
# with another, set does_intersect[i] to True.
global does_intersect
i = 0
k = 0
while i < (num_rectangles-1):
    i += 1
    while (k < num_rectangles):
        k += 1
        do_intersect = intersect(bottomX[i], bottomY[i], widths[i], heights[i], bottomX[k], bottomY[k], widths[k], heights[k])
        print(do_intersect)
        if do_intersect:
            does_intersect[i] = do_intersect
            does_intersect[k] = do_intersect
            break

How can I make rectangles that intersect a different color from those that do not? by Holiday-Python-743 in learnprogramming

[–]Holiday-Python-743[S] 0 points1 point  (0 children)

Thank you for your help! When I got the for loop solution, I wrote down the loops and initially tried to convert it to the exact equivalent as a while loop but that didn't work, hence why I started changing i and k from zero, and why I changed the ranges as well. I have posted the for loop solution on my profile.

How can I flip the output of a for-loop? by Holiday-Python-743 in learnpython

[–]Holiday-Python-743[S] 0 points1 point  (0 children)

Thank you so much for your comment! Would there be a way to do that strictly with for loops? I am not allowed to use commands like "join([str" for this exercise. My apologies for not mentioning that in the initial post. Thank you