2014-07-15 99 views
-4

我刚刚编写的代码用于在每次最终用户滚动时调整列表中每个项目的值,每个项目的位置匹配其相应对象的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 
+9

请问您是否可以将这个问题简化为一个[最小示例](http://stackoverflow.com/help/mcve)并更清楚地说明问题的可疑来源? – jonrsharpe

+4

'control2'在那个'if'语句中总是为零 - 它早先设置为零。 – DNA

回答

4

你有一个嵌套的if语句这样control2设置为0

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 # set to 0 here 
     if control != control2: # is 0 here 

控制2决不会15或在if语句比0以外的任何值。

+0

啊,我以为这只是错误的逻辑。谢谢。 – RoyalSwish

+0

没问题,不客气。 –