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
#21
When I post, I make a copy and cut away anything not directly related to the problem. Quite often the act of preparing code to post reveals the source of my problem. This step is always worth doing.

Not to be mean, but the code in your last post is worthless. Not only is there no way I can run this code, but it doesn't even include any reference to your earlier posts. Where in this method did you put an if statement that should block out some code? Even if you don't post code I can run, it would be useful to see you if statement in the context of the code where it is used.

To demonstrate how I go about authoring a post I will assume this is some code I wrote. and I am having problems with this part:
 if len(data) > 0:
    student.datadict.add(cl_note.date, data)
else:
    continue
I copy the method, provide just enough structure to run it, and cut out everything I don't need. I end up with something like this:
class Stuff:
    def getNotesByStudent(self):
        student = {}
        first_entry_done = False
        for cl_note in "ABC":
            for seat_num in [1]:
                data = {}
                if True:
                    if not first_entry_done:
                        data = {"key": "value"}
                        first_entry_done = True
                    if len(data) > 0:
                        student.update(data)
                    else:
                        continue
        return student

print(Stuff().getNotesByStudent())
If this demostrates my problem, I post the code, describe what it is supposed to do, describe what it is doing, and provide any instructions needed to make the code demonstrate the odd behavior. I also list what I have done to try and diagnose the problem.

I don't think all that is required in this case, but could you point out where you were putting your if statement. Preferrably editing the code to include the if statement that isn't acting like an if statement?
ndc85430 likes this post
Reply
#22
Quote:Not to be mean, but the code in your last post is worthless. Not only is there no way I can run this code, but it doesn't even include any reference to your earlier posts. Where in this method did you put an if statement that should block out some code? Even if you don't post code I can run, it would be useful to see you if statement in the context of the code where it is used.
In my opinion, the problem is that if you don't post the whole code and only create snippet for specific points, it is not easy to find the issue because this could be related to previous method executions which can only be found via the stack. Sure, the previous posted code isn't complete and so it can't be run, but I wanted to show you the overall code (based on it's structure) of the mentioned method.

(Jan-16-2023, 07:18 PM)deanhystad Wrote: When I post, I make a copy and cut away anything not directly related to the problem. Quite often the act of preparing code to post reveals the source of my problem. This step is always worth doing.

Not to be mean, but the code in your last post is worthless. Not only is there no way I can run this code, but it doesn't even include any reference to your earlier posts. Where in this method did you put an if statement that should block out some code? Even if you don't post code I can run, it would be useful to see you if statement in the context of the code where it is used.

To demonstrate how I go about authoring a post I will assume this is some code I wrote. and I am having problems with this part:
 if len(data) > 0:
    student.datadict.add(cl_note.date, data)
else:
    continue
I copy the method, provide just enough structure to run it, and cut out everything I don't need. I end up with something like this:
class Stuff:
    def getNotesByStudent(self):
        student = {}
        first_entry_done = False
        for cl_note in "ABC":
            for seat_num in [1]:
                data = {}
                if True:
                    if not first_entry_done:
                        data = {"key": "value"}
                        first_entry_done = True
                    if len(data) > 0:
                        student.update(data)
                    else:
                        continue
        return student

print(Stuff().getNotesByStudent())
If this demostrates my problem, I post the code, describe what it is supposed to do, describe what it is doing, and provide any instructions needed to make the code demonstrate the odd behavior. I also list what I have done to try and diagnose the problem.

I don't think all that is required in this case, but could you point out where you were putting your if statement. Preferrably editing the code to include the if statement that isn't acting like an if statement?
You have put the if statement which does not work on the right place which is on line 12-15 if you mean that. In that case, I have observed that removing the else statement on line 14 the if statement will not be executed, which in my opinon doesn't make any sense. In other case, when I put it back the code gets executed properly. As I wrote before this does not make sense to me, because you don't need to have an else statement in general so the if statement gets executed properly (this is the first time I have encountered this problem and I have also developed in C++ and Java and this behavior does not occur there).
Reply
#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
#24
This is what I have also encountered after trying lots and lots of things in VS Code and the debugger, that it doesn't behave well at anytime. Your example shows this also. In my opinion the debugger needs serious updates in the future and I hope these kind of issues will be fixed as best as it can. Nevertheless, it calms me down that not only I encounter such wired behaviors, but others, too. Nevertheless, thank you very much for spending so much time to help me out. One last thing do you believe that pycharm is a better choice? I have used it in past, but it consumes a lot of resources on my laptop. In my opinion, the debugger of pycharm behave better.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logger behaviour setdetnet 1 896 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 729 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,262 Apr-09-2023, 01:48 AM
Last Post: vugz
  Generator behaviour bla123bla 2 1,111 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  Inconsistent behaviour in output - web scraping Steve 6 2,564 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  IWhat is the cause to get XPath in weird format using Python? MDRI 7 3,707 May-27-2021, 02:01 AM
Last Post: MDRI
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,724 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Behaviour of 2D array SimonB 6 2,842 Jan-21-2021, 01:29 PM
Last Post: SimonB
  strange behaviour- plotting nathan_Blanc_Haifa 0 1,506 Dec-27-2020, 01:37 PM
Last Post: nathan_Blanc_Haifa
  OOP behaviour problem JohnB 3 2,435 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