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
#11
To paraphrase, the original question is "I have an if statement. Even though the condition is false, the statement is executed".

You have not demonstrated this problem. In you posted code, the condition is False, but the statement is not executed. Can you provide an example where the statement is false, but the statement does get executed?
Reply
#12
Guys, after trying the solution of @Yoriz i.e. this:

print(1 == 2)
if 1 == 2:
    print("[...]")
print("Ended")
The code works properly, which is wired because only adding the last print("Ended") part changes the behavior (i.e. the if statement gets not executed). This does not make any sense to me.
Reply
#13
(Jan-15-2023, 06:02 PM)deanhystad Wrote: To paraphrase, the original question is "I have an if statement. Even though the condition is false, the statement is executed".

You have not demonstrated this problem. In you posted code, the condition is False, but the statement is not executed. Can you provide an example where the statement is false, but the statement does get executed?
I have uploaded a new screenshot to demonstrate this.
Reply
#14
(Jan-15-2023, 05:59 PM)mikepy Wrote:
(Jan-15-2023, 05:16 PM)deanhystad Wrote: You can paste images. I've seen it done. But for nearly every kind of discussion about programs and errors, text is superior to a screen shot. Here, for example. I ran your code and it does not print [...]. I am not running Python 3.10.8, but I ran it on 3.10.4 and 3.11, and there are no reported logic or comparison problems for 3.10.8.

Please post an example that demonstrates the behavior you are seeing.
I have tried it before but the image was broken. So I have tried another online provider now to upload my screenshot. As you can see the code gets executed (yellow part)
[Image: test.png]
How else could I demonstrate the wrong behavior including the debugging process?
Reply
#15
The best way to demonstrate the wrong behavior is to produce an example that allows others to reproduce what you are seeing. So far the only way I can reproduce what is shown in your screenshot is to edit the code condition (change from True to False) after stepping to the print statement.

If you cannot reproduce the behavior in a small example program, it usually means the problem is not what you think it is. I see this is line 92. Maybe there is something odd going on in lines 1-90. I cannot imagine what that might be, but there is nothing wrong with the two lines you posted.

How are you running the debugger? Just using the Run menu -> Start Debugging? What is your debugging configuration?
ndc85430 likes this post
Reply
#16
(Jan-15-2023, 06:31 PM)deanhystad Wrote: The best way to demonstrate the wrong behavior is to produce an example that allows others to reproduce what you are seeing. So far the only way I can reproduce what is shown in your screenshot is to edit the code condition (change from True to False) after stepping to the print statement.

If you cannot reproduce the behavior in a small example program, it usually means the problem is not what you think it is. I see this is line 92. Maybe there is something odd going on in lines 1-90. I cannot imagine what that might be, but there is nothing wrong with the two lines you posted.

How are you running the debugger? Just using the Run menu -> Start Debugging? What is your debugging configuration?
Exactly, I run the debugger as you mentioned. I will check the debug configuration and maybe there is something wrong there. I will post if I have fixed it. BTW what are the most important configuration in VS Code for the python debugger?

EDIT:
As I can observe the problem is related to VS Code, because when I execute the code in the terminal (Ubuntu 20.04.LTS), the code runs properly. I will check further the settings and come back later.
Reply
#17
I use a very simple configuration when running the "current" file.
Quote: "configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
},
Does the code execute correctly if you run it from the terminal in VSCode? Does the code run correctly if you use the run button in VSCode?
Reply
#18
(Jan-15-2023, 07:13 PM)deanhystad Wrote: I use a very simple configuration when running the "current" file.
Quote: "configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
},
Does the code execute correctly if you run it from the terminal in VSCode? Does the code run correctly if you use the run button in VSCode?
THX, I use the same settings in VS Code. The same behavior occurs in both terminal and VSCode.
Reply
#19
What do you mean by "The same behavior occurs in both terminal and VSCode." You demonstrated the VSCode behavior using a screenshot of a debugging session. You cannot see that when running your code from the terminal. Or do you mean that running the code from the VSCode terminal behaves the same as running the code from Ubuntu terminal?

Could you post the code? I'm interested to see if it does the same thing on my machine.
Reply
#20
(Jan-16-2023, 05:10 PM)deanhystad Wrote: What do you mean by "The same behavior occurs in both terminal and VSCode." You demonstrated the VSCode behavior using a screenshot of a debugging session. You cannot see that when running your code from the terminal. Or do you mean that running the code from the VSCode terminal behaves the same as running the code from Ubuntu terminal?

Could you post the code? I'm interested to see if it does the same thing on my machine.
>> Or do you mean that running the code from the VSCode terminal behaves the same as running the code from Ubuntu terminal?
That is exactly what I mean.

To be honest I don't know how you can check the overall code, because the code acts with other classes, lists etc. Nevertheless, I have posted the code of the method in which I have the issue. Maybe you can get this running on your system by adjusting the code. I have added some comments to show more information.

def getNotesByStudent(self, surname, name):
        ''' Retrieves all notes based on the students surname and name '''
        first_entry_done = False
        student = Student() # This is a class

        # Finds all student data by surname and name
        for cl_note in self.__class_notes: # This is a list of note classes 
            for seat_num in cl_note.note.index.values:
                data = {}
                # Checks for the desired surname and name
                if cl_note.note.loc[seat_num, ['Surname']].values == surname and cl_note.note.loc[seat_num, ['Name']].values == name: # note is a node class containing a pandas dataframe
                    # Sets specific data only on the first iteration
                    if not first_entry_done:
                        student.seat_num = {'seatnr': int(np.all(cl_note.note.loc[seat_num, ['Seat_Num']].values))}
                        student.surname = {'surname': np.all(cl_note.note.loc[seat_num, ['Surname']].values)}
                        student.name = {'name': np.all(cl_note.note.loc[seat_num, ['Name']].values)}
                        student.multiple_data = True
                        first_entry_done = True

                    if np.all(cl_note.note.loc[seat_num, ['Participations']].values) is not None:
                        data['participation'] = np.all(cl_note.note.loc[seat_num, ['Participations']].values)
                    if np.any(cl_note.note.loc[seat_num, ['Tests']].values) is not None:
                        data['tests'] = np.all(cl_note.note.loc[seat_num, ['Tests']].values)
                    if np.all(cl_note.note.loc[seat_num, ['Exercises']].values) is not None:
                        if math.isnan(np.all(cl_note.note.loc[seat_num, ['Exercises']].values)) is False:
                            data['exercises'] = np.all(cl_note.note.loc[seat_num, ['Exercises']].values)
                    if np.all(cl_note.note.loc[seat_num, ['Predefined_Comments']].values) is not None:
                        data['predefined_comments'] = np.all(cl_note.note.loc[seat_num, ['Predefined_Comments']].values)
                    if np.all(cl_note.note.loc[seat_num, ['Other_Comments']].values) is not None:
                        data['other_comments'] = np.all(cl_note.note.loc[seat_num, ['Other_Comments']].values)
                    if np.all(cl_note.note.loc[seat_num, ['Grade']].values) is not None:
                        if math.isnan(np.all(cl_note.note.loc[seat_num, ['Grade']].values)) is False:
                            data['grade'] = np.all(cl_note.note.loc[seat_num, ['Grade']].values)

                    # Adds only the additional data if there is an entry available in the current date
                    if len(data) > 0:
                        student.datadict.add(cl_note.date, data)
                    else:
                      continue

        return student
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logger behaviour setdetnet 1 899 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 732 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,266 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,569 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  IWhat is the cause to get XPath in weird format using Python? MDRI 7 3,712 May-27-2021, 02:01 AM
Last Post: MDRI
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,726 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Behaviour of 2D array SimonB 6 2,843 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,436 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