我剛剛編寫的代碼用於在每次最終用戶滾動時調整列表中每個項目的值,每個項目的位置匹配其相應對象的y座標。Python - 如果語句在我不想要的時候返回True True
我的代碼背後的邏輯看看當滾動時,原始y座標和對象的新y座標之間的差異是否保持不變,如果不是,那麼它應該繼續執行for
循環,否則繼續代碼在if
聲明之後。
由於某些原因,if
語句不斷返回True
,即使我在使用的兩個控制變量之間沒有差異。
我不知道我是否忽略了一些微妙的東西,意味着它會繼續返回True
或者我的邏輯沒有像我期望的那樣工作;我打賭的是後者。
#example y values in list
yIndex = [122, 152, 212, 242]
scroll_y = 63 #initial y scroll coordinate
originalScroll_y = 63
while True:
for event in pygame.event.get():
#code checking whether user has scrolled
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4 and scroll_y != 63:
originalScroll_y = scroll_y
scroll_y = min(scroll_y + 15, 63)
if event.button == 5:
originalScroll_y = scroll_y
scroll_y = max(scroll_y - 15, finalY + 470)
elif event.type == MOUSEBUTTONUP and event.button == 1 and isinstance(page, MainPage):
x, y = pygame.mouse.get_pos()
control = originalScroll_y - scroll_y
control2 = 0
#e.g. if control = 15 and control2 = 15, it returns True instead
#of False
if control != control2:
for k, j in enumerate(yIndex):
j -= control
yIndex[k] = j
control2 = control
for i in yIndex:
if y >= (i - 10) and y <= (i + 20):
if i in indexedContacts:
buttonSound.play()
sleep(0.5)
scroll_y = 63
page = EditPage()
page.style()
page.contactFields()
break
請問您是否可以將這個問題簡化爲一個[最小示例](http://stackoverflow.com/help/mcve)並更清楚地說明問題的可疑來源? – jonrsharpe
'control2'在那個'if'語句中總是爲零 - 它早先設置爲零。 – DNA