Python Forum
Weird behaviour using if statement in python 3.10.8
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird behaviour using if statement in python 3.10.8
#23
I was able to make your "problem" occur using the code below.
def func():
    for i in range(3):
        if i == 0:
            print("hi")
            print("lo")


func()
Run the code in the debugger. Set a breakpoint on line 2. Run the program and begin single stepping. The first time through the loop the program prints "hi" and "lo". The second time though the loop the command cursor steps to line 5 instead of jumping from line 3 to line 2. However, the program does not print. This appears to be some kind of debugger artifact. The command cursor always advances to a command following an if statement. When the if statement is at the end of a loop, the command cursor steps to the last command in the if statement body, even if the condition is not true.

Weird. I guess the lesson is don't get too hung up on how things look. I would not want to guess how many posts on this forum come down to your IDE not behaving how you expect. "I want to print my enormous table, but my program only prints out the first 5 and last 5 rows.columns." "I have a raw string with backslashes, but when I print it out I have double backslashes." "My debugger steps to the line following an if statement even if the if statement is false." Before getting too wound up, verify that what you see is real. Run your program outside the IDE and see if it only prints part of your big table. Check the length of your string to verify that the double backslashes are real. Verify that the debugger executes, or doesn't execute, that command that it shouldn't because the if condition was false.
Reply


Messages In This Thread
RE: Weird behaviour using if statement in python 3.10.8 - by deanhystad - Jan-17-2023, 06:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  logger behaviour setdetnet 1 932 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 772 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,323 Apr-09-2023, 01:48 AM
Last Post: vugz
  Generator behaviour bla123bla 2 1,151 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  Inconsistent behaviour in output - web scraping Steve 6 2,641 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  IWhat is the cause to get XPath in weird format using Python? MDRI 7 3,799 May-27-2021, 02:01 AM
Last Post: MDRI
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,799 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Behaviour of 2D array SimonB 6 2,914 Jan-21-2021, 01:29 PM
Last Post: SimonB
  strange behaviour- plotting nathan_Blanc_Haifa 0 1,526 Dec-27-2020, 01:37 PM
Last Post: nathan_Blanc_Haifa
  OOP behaviour problem JohnB 3 2,465 Aug-18-2020, 07:51 PM
Last Post: JohnB

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020