英文:
Python code not iterating through more than one item in list
问题
以下是代码部分的中文翻译:
x_count = 0
y_count = 0
num = 1
painted = []
while num < len(hex_codes):
color = hex_codes[num-1]
if color not in painted:
if color == "#ffffff":
color = hex_codes[num]
select_color(color)
time.sleep(0.02)
for code in hex_codes:
print("Color: " + color + ", Code: " + code)
if code == color:
paint_at(x_count, y_count)
x_count += 1
if x_count == 32:
x_count = 0
y_count += 1
if y_count == 32:
break
painted.append(color)
num += 1
请注意,代码中的HTML实体编码(如<
和"
)没有被翻译,因为它们是HTML编码,不需要在代码中更改。
英文:
I am making a self painting program. paint_at() paints the selected color at x and y coordinates (stating at 0, ending at 31). select_color() selects the color in hex format. The issue comes in with the for code in hex_codes:
loop. what I think is happening is that it doesn't iterate more than one pixel if it isn't an instant match. I don't want this. Instead, I want it to cycle through all the codes in the painting, and paint only the matches. Below is the output of each iteration of the loop. As you can see, #000000 checks everything, while the rest give up after one pixel. Is anyone able to figure out what's going on?
Color: #000000, Code: #75403a
Color: #000000, Code: #b57051
Color: #000000, Code: #753e39
Color: #000000, Code: #b86f4f
Color: #000000, Code: #754038
Color: #000000, Code: #e3a870
Color: #000000, Code: #6a4437
Color: #000000, Code: #347243
Color: #000000, Code: #7a9a5b
Color: #000000, Code: #347241
Color: #000000, Code: #67c451
Color: #000000, Code: #7c9c5b
Color: #000100, Code: #000000
Color: #020200, Code: #000000
Color: #010300, Code: #000000
Color: #020401, Code: #000000
Color: #000501, Code: #000000
Color: #040402, Code: #000000
My code:
x_count = 0
y_count = 0
num = 1
painted = []
while num < len(hex_codes):
color = hex_codes[num-1]
if color not in painted:
if color == "#ffffff":
color = hex_codes[num]
select_color(color)
time.sleep(0.02)
for code in hex_codes:
print("Color: " + color + ", Code: " + code)
if code == color:
paint_at(x_count, y_count)
x_count += 1
if x_count == 32:
x_count = 0
y_count += 1
if y_count == 32:
break
painted.append(color)
num +=1
答案1
得分: 0
我认为当内循环完成时,x_count
和 y_count
变量没有被重置为它们的初始值,这导致程序继续从上一个颜色的最后一个像素开始绘制,而不是从左上角开始。
为了解决这个问题,你可以在内循环完成后将 x_count
和 y_count
重置为 0。这可以确保程序为每种颜色从左上角开始绘制。
painted = []
for code in hex_codes:
if code not in painted:
select_color(code)
time.sleep(0.02)
for i in range(32 * 32):
if hex_codes[i] == code:
paint_at(x_count, y_count)
x_count += 1
if x_count == 32:
x_count = 0
y_count += 1
if y_count == 32:
break
painted.append(code)
x_count = 0
y_count = 0
英文:
I think when the inner loop finishes, the x_count
and y_count
variables are not being reset to their initial values, which causes the program to continue painting from the last pixel of the previous color instead of starting from the top left corner.
To fix this, you can reset x_count
and y_count
to 0 after the inner loop finishes. This ensures that the program starts painting from the top left corner for each color.
painted = []
for code in hex_codes:
if code not in painted:
select_color(code)
time.sleep(0.02)
for i in range(32 * 32):
if hex_codes[i] == code:
paint_at(x_count, y_count)
x_count += 1
if x_count == 32:
x_count = 0
y_count += 1
if y_count == 32:
break
painted.append(code)
x_count = 0
y_count = 0
答案2
得分: 0
一个显而易见的问题是,在循环迭代时,你在未找到匹配的初始像素的情况下,不断地增加 x_count 和 y_count 变量的值,而没有将它们重置为每种新颜色的初始值。换句话说,如果在初始像素上未找到匹配项,循环将继续使用错误的坐标搜索后续的像素。
也许你应该尝试这样做,在循环的每次迭代开始时将 x_count 和 y_count 重置为 0:
for code in hex_codes:
x_count = 0
y_count = 0
print("Color: " + color + ", Code: " + code)
if code == color:
paint_at(x_count, y_count)
x_count += 1
if x_count == 32:
x_count = 0
y_count += 1
if y_count == 32:
break
希望这能帮到你,谢谢!
英文:
One problem that sticks out is the fact that when you are iterating through the loop, you are incrementing the x_count and y_count variables without returning them to their initial values for each new color. The loop will continue to search successive pixels using the erroneous coordinates if a match is not found on the initial pixel, in other words.
Maybe you should try this, you should reset x_count and y_count to 0 at the beginning of each iteration of the loop:
for code in hex_codes:
x_count = 0
y_count = 0
print("Color: " + color + ", Code: " + code)
if code == color:
paint_at(x_count, y_count)
x_count += 1
if x_count == 32:
x_count = 0
y_count += 1
if y_count == 32:
break
I hope this helps thanks!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论